Skip to content

FillDepressions panics with "Error unwrapping 'output'" due to threading race condition #476

@joostsijm

Description

@joostsijm

FillDepressions crashes intermittently during filling depressions. Here my basic triage.

The error:

thread 'main' panicked at whitebox-tools-app/src/tools/hydro_analysis/fill_depressions.rs:368:23:
Error unwrapping 'output'

How to reproduce (on multicore system):

./whitebox_tools --run="FillDepressions" --dem='input.tif' --output='output.tif' --fix_flats -v

The workaround for me is to force single-threaded execution via Python API:

wbt.set_max_procs(1)

I think this relates to a race condition at line 368, in fill_depressions.rs.
The code spawns threads (lines ~310-350) that each clone Arc<output2>. After receiving channel results, the main thread attempts Arc::try_unwrap(output2) at line 368.
However, spawned threads may not have finished executing, so their Arc clones are still alive, causing the unwrap to fail.
The issue: receiving data via mpsc::channel does not guarantee thread completion.

Suggested solution is to store thread handles and join them before unwrapping.
Then this ensures all Arc clones are dropped before attempting to unwrap.

// Replace lines ~310-313:
let mut handles = vec![];
for tid in 0..num_procs {
    let output2 = output2.clone();
    let tx = tx.clone();
    let handle = thread::spawn(move || {
        // ... existing thread code ...
    });
    handles.push(handle);
}

// Add before line 365 (before the Arc unwrap):
for handle in handles {
    handle.join().expect("Thread panicked");
}

// Now the existing unwrap at line 368 will succeed
output = match Arc::try_unwrap(output2) {
    Ok(val) => val,
    Err(_) => panic!("Error unwrapping 'output'"),
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions