Skip to content

Fix SAM3 visual segment concurrency race (DATAMAN-183)#2124

Open
digaobarbosa wants to merge 2 commits intomainfrom
dataman-183-fix-sam3-race-condition
Open

Fix SAM3 visual segment concurrency race (DATAMAN-183)#2124
digaobarbosa wants to merge 2 commits intomainfrom
dataman-183-fix-sam3-race-condition

Conversation

@digaobarbosa
Copy link
Contributor

@digaobarbosa digaobarbosa commented Mar 18, 2026

Description

Fix concurrency race condition in /sam3/visual_segment endpoint causing "An image must be set with .set_image(...) before mask prediction" errors in production.

Root cause: predict_inst() in the sam3 library internally manages predictor state (_is_image_set, _features) on a shared inst_interactive_predictor instance. It sets _is_image_set = True, calls predict(), then clears _is_image_set = False. With concurrent requests, one thread's cleanup can execute between another thread's state-set and the state-check inside _predict(), causing the RuntimeError.

Fix: Wrap predict_inst() call in the existing _state_lock (RLock) to serialize access and prevent concurrent state corruption.

Logs in prod indicate this error always appear before the 500 in visual_segment

Type of change

  • Bug fix

How has this change been tested?

Draft - testing in progress.

Will the change affect Universe?

No

Any specific deployment considerations

None - backward compatible, no API changes.

Docs

  • Docs updated? N/A

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.

1 participant