Skip to content

Fix race condition leading to deadlock in vtkDataEncoder

This change should fix race condition in vtkSharedData::GetNextInputToProcess and vtkDataEncoder::vtkInternals::TerminateAllWorkers which may lead to deadlock.

This deadlock may happen when multiple worker threads are requested, but they're scheduled differently for some reason.

In my case 3 threads are requested, but only 2 have started working before vtkDataEncoder::vtkInternals::TerminateAllWorkers calls vtkSharedData::RequestAndWaitForWorkersToEnd.

In that case active threads counter is only increased twice, and vtkSharedData::RequestAndWaitForWorkersToEnd finishes when only 2 out of 3 threads finished working.

Since in that case vtkSharedData::Done is set to 'false', when last worker thread is started, it increases active threads counter, checks that vtkSharedData::Done is 'false' and waits for input forever.

At the same time vtkDataEncoder::vtkInternals::TerminateAllWorkers in main thread waits for last worker thread to finish working.

This change updates handling of vtkSharedData::ActiveThreadCount to make such race caused by scheduling not possible.

In addition to that, signalling condition variables is protected by mutex too, DoneLock is replaced by using InputLock too, and Done is reset to 'false' only when new worker threads are requested.

Merge request reports