Skip to content

Fix lock ordering deadlock in StateManager#55

Merged
kalabukdima merged 2 commits into
subsquid:masterfrom
PolyProgrammist:fix-deadlock
May 18, 2026
Merged

Fix lock ordering deadlock in StateManager#55
kalabukdima merged 2 commits into
subsquid:masterfrom
PolyProgrammist:fix-deadlock

Conversation

@PolyProgrammist

Copy link
Copy Markdown
Contributor

set_desired_chunks acquired state before datasets_index, while run() acquires them in the opposite order — a classic AB-BA pattern that could deadlock when a new assignment arrives concurrently with the download loop. Align the order with run() (datasets_index then state).

Introduced in #38 when set_desired_chunks and set_datasets_index were merged into one function without aligning lock acquisition order.

set_desired_chunks acquired state before datasets_index, while run()
acquires them in the opposite order — a classic AB-BA pattern that
could deadlock when a new assignment arrives concurrently with the
download loop. Align the order with run() (datasets_index then state).

Introduced in subsquid#38 when set_desired_chunks and set_datasets_index were
merged into one function without aligning lock acquisition order.
@kalabukdima

Copy link
Copy Markdown
Contributor

Great catch! It may explain why we observed hanging subroutines occasionally. Thank you for the contribution!

I would suggest to also change the order of these fields in the StateManager struct. Usually we try to lock in the order of declaration to avoid such issues, but this one sneaked in.

@kalabukdima kalabukdima self-requested a review May 12, 2026 13:33
@PolyProgrammist

Copy link
Copy Markdown
Contributor Author

@kalabukdima done, please take a look

@kalabukdima kalabukdima merged commit c674f35 into subsquid:master May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants