Skip to content

fix(imagebuf): fix data race that could corrupt multithreaded reads - #5325

Merged
lgritz merged 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-ibrace
Jul 30, 2026
Merged

fix(imagebuf): fix data race that could corrupt multithreaded reads#5325
lgritz merged 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-ibrace

Conversation

@lgritz

@lgritz lgritz commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

m_pixels_valid was set true by realloc() as soon as the pixel buffer was allocated, before read() actually filled it with file data. Since validate_pixels() checks the flag lock-free before falling back to the mutex, a second thread (e.g. another computePixelHashSHA1 block) could see it as true and read a still-empty buffer. init_spec() had the same issue with m_spec_valid, set before all spec fields were written.

Move both flags to be set only after their data is fully populated, and make them (plus m_pixels_read, read via the public ImageBuf::pixels_read() with no lock) atomic so their lock-free readers get a proper memory barrier.

Fixes #5324

Assisted-by: Claude Code / Sonnet 5

@lgritz

lgritz commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

The Mac CI failures are unrelated to this PR -- looks like GHA Mac runners updated their OpenCV and it's not compatible with what we're looking for.

m_pixels_valid was set true by realloc() as soon as the pixel buffer
was allocated, before read() actually filled it with file data. Since
validate_pixels() checks the flag lock-free before falling back to the
mutex, a second thread (e.g. another computePixelHashSHA1 block) could
see it as true and read a still-empty buffer. init_spec() had the same
issue with m_spec_valid, set before all spec fields were written.

Move both flags to be set only after their data is fully populated,
and make them (plus m_pixels_read, read via the public
ImageBuf::pixels_read() with no lock) atomic so their lock-free
readers get a proper memory barrier.

Assisted-by: Claude Code / Sonnet 5

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz

lgritz commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

Comments?

@lgritz

lgritz commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

No objections -> merging

@lgritz
lgritz merged commit b2ef3ed into AcademySoftwareFoundation:main Jul 30, 2026
29 checks passed
@lgritz
lgritz deleted the lg-ibrace branch July 30, 2026 00:59
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Jul 30, 2026
…cademySoftwareFoundation#5325)

m_pixels_valid was set true by realloc() as soon as the pixel buffer was
allocated, before read() actually filled it with file data. Since
validate_pixels() checks the flag lock-free before falling back to the
mutex, a second thread (e.g. another computePixelHashSHA1 block) could
see it as true and read a still-empty buffer. init_spec() had the same
issue with m_spec_valid, set before all spec fields were written.

Move both flags to be set only after their data is fully populated, and
make them (plus m_pixels_read, read via the public
ImageBuf::pixels_read() with no lock) atomic so their lock-free readers
get a proper memory barrier.

Fixes AcademySoftwareFoundation#5324

Assisted-by: Claude Code / Sonnet 5

Signed-off-by: Larry Gritz <lg@larrygritz.com>
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Jul 30, 2026
…cademySoftwareFoundation#5325)

m_pixels_valid was set true by realloc() as soon as the pixel buffer was
allocated, before read() actually filled it with file data. Since
validate_pixels() checks the flag lock-free before falling back to the
mutex, a second thread (e.g. another computePixelHashSHA1 block) could
see it as true and read a still-empty buffer. init_spec() had the same
issue with m_spec_valid, set before all spec fields were written.

Move both flags to be set only after their data is fully populated, and
make them (plus m_pixels_read, read via the public
ImageBuf::pixels_read() with no lock) atomic so their lock-free readers
get a proper memory barrier.

Fixes AcademySoftwareFoundation#5324

Assisted-by: Claude Code / Sonnet 5

Signed-off-by: Larry Gritz <lg@larrygritz.com>
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.

bug: computePixelHashSHA1 is unstable when multithreaded

1 participant