Skip to content

improve thread safety for concurrent tiff loads #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

angusdavisadobe
Copy link
Contributor

Description

This fixes a thread-safety issue with concurrent parsing of multiple tiff files. The code uses several static std::unordered_maps and the indexing operator[] was used to perform the lookup. Use of this operator has the side effect of adding a default value to the map if the key was not found. This is not what should happen and isn't thread safe. The proper fix is to make the maps const and use the find() method of the map to perform the lookup and return a default value if not found.

Tests

Tests were not performed. However the code changes were tested within the context of loading USD files with multiple tiff files.

Checklist:

  • [X ] I have read the contribution guidelines.
  • If this is more extensive than a small change to existing code, I
    have previously submitted a Contributor License Agreement
    (individual, and if there is any way my
    employers might think my programming belongs to them, then also
    corporate).
  • I have updated the documentation, if applicable.
  • I have ensured that the change is tested somewhere in the testsuite
    (adding new test cases if necessary).
  • [X ] My code follows the prevailing code style of this project.

// return color_space_names[color_space];
auto it = color_space_names.find(color_space);
return (it != color_space_names.end()) ? it->second : nullptr;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get rid of the extra blank line here, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the blank line.

Copy link
Collaborator

@lgritz lgritz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, good catch. It did not occur to me that it might be queried with a key that wasn't already in the map, thus potentially altering it and breaking thread safety. I should have declared these const all along.

The whole thing LGTM except for the syntax error I pointed out with the missing brace.

@lgritz lgritz force-pushed the angudavi/improve-thread-safety-for-concurrent-tiff-loads branch from 053453e to adc9705 Compare February 10, 2023 19:49
Copy link
Collaborator

@lgritz lgritz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lgritz lgritz added this pull request to the merge queue Feb 10, 2023
Merged via the queue into AcademySoftwareFoundation:master with commit 9cb000f Feb 10, 2023
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Feb 11, 2023
…ation#3767)

* Make lookups of const unordered_maps thread-safe

* added back deleted end brace

* clang-format

---------

Co-authored-by: Larry Gritz <[email protected]>
lgritz added a commit to lgritz/OpenImageIO that referenced this pull request Mar 8, 2023
…ation#3767)

* Make lookups of const unordered_maps thread-safe

* added back deleted end brace

* clang-format

---------

Co-authored-by: Larry Gritz <[email protected]>
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