-
Notifications
You must be signed in to change notification settings - Fork 137
8362252: [lworld] Possible synchronization issue in jdk/internal/jimage/ImageReader.java #1828
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
base: lworld
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -266,24 +266,25 @@ public int hashCode() { | |
| } | ||
| } | ||
|
|
||
| // Map of currently open shared-readers. | ||
| private static final Map<ReaderKey, SharedImageReader> OPEN_FILES = new HashMap<>(); | ||
|
|
||
| // List of openers for this shared image. | ||
| // List of openers for this shared image. Synchronized on OPEN_FILES. | ||
| private final Set<ImageReader> openers = new HashSet<>(); | ||
|
|
||
| // Attributes of the jimage file. The jimage file does not contain | ||
| // attributes for the individual resources (yet). We use attributes | ||
| // of the jimage file itself (creation, modification, access times). | ||
| private final BasicFileAttributes imageFileAttributes; | ||
|
|
||
| // Cache of all user visible nodes, guarded by synchronizing 'this' instance. | ||
| // Cache of all user visible nodes. Synchronized on 'this'. | ||
| private final Map<String, Node> nodes; | ||
|
|
||
| // Preview mode support. | ||
| private final boolean previewMode; | ||
| // A relativized mapping from non-preview name to directories containing | ||
| // preview-only nodes. This is used to add preview-only content to | ||
| // directories as they are completed. | ||
| // directories as they are completed. Synchronized on 'this'. | ||
| private final HashMap<String, Directory> previewDirectoriesToMerge; | ||
|
|
||
| private SharedImageReader(Path imagePath, ByteOrder byteOrder, boolean previewMode) throws IOException { | ||
|
|
@@ -405,18 +406,18 @@ private static ImageReader open(Path imagePath, ByteOrder byteOrder, boolean pre | |
|
|
||
| synchronized (OPEN_FILES) { | ||
| ReaderKey key = new ReaderKey(imagePath, previewMode); | ||
| SharedImageReader reader = OPEN_FILES.get(key); | ||
| SharedImageReader sharedReader = OPEN_FILES.get(key); | ||
|
|
||
| if (reader == null) { | ||
| if (sharedReader == null) { | ||
| // Will fail with an IOException if wrong byteOrder. | ||
| reader = new SharedImageReader(imagePath, byteOrder, previewMode); | ||
| OPEN_FILES.put(key, reader); | ||
| } else if (reader.getByteOrder() != byteOrder) { | ||
| throw new IOException("\"" + reader.getName() + "\" is not an image file"); | ||
| sharedReader = new SharedImageReader(imagePath, byteOrder, previewMode); | ||
| OPEN_FILES.put(key, sharedReader); | ||
| } else if (sharedReader.getByteOrder() != byteOrder) { | ||
| throw new IOException("\"" + sharedReader.getName() + "\" is not an image file"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its pre-existing, but ... |
||
| } | ||
|
|
||
| ImageReader image = new ImageReader(reader); | ||
| reader.openers.add(image); | ||
| ImageReader image = new ImageReader(sharedReader); | ||
| sharedReader.openers.add(image); | ||
|
|
||
| return image; | ||
| } | ||
|
|
@@ -425,19 +426,22 @@ private static ImageReader open(Path imagePath, ByteOrder byteOrder, boolean pre | |
| public void close(ImageReader image) throws IOException { | ||
| Objects.requireNonNull(image); | ||
|
|
||
| boolean shouldCloseSharedReader; | ||
| synchronized (OPEN_FILES) { | ||
| if (!openers.remove(image)) { | ||
| throw new IOException("image file already closed"); | ||
| } | ||
|
|
||
| if (openers.isEmpty()) { | ||
| close(); | ||
| shouldCloseSharedReader = openers.isEmpty(); | ||
| if (shouldCloseSharedReader | ||
| && !OPEN_FILES.remove(new ReaderKey(getImagePath(), previewMode), this)) { | ||
| throw new IOException("image file not found in open list"); | ||
| } | ||
| } | ||
| if (shouldCloseSharedReader) { | ||
| synchronized (this) { | ||
| nodes.clear(); | ||
|
|
||
| if (!OPEN_FILES.remove(new ReaderKey(getImagePath(), previewMode), this)) { | ||
| throw new IOException("image file not found in open list"); | ||
| } | ||
| } | ||
| super.close(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will double check if moving this outside the synchronization of OPEN_FILES is an issue. |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the name of this for clarity. It's a bit confusing to have two "image reader" instances in scope with one called "image" and one called just "reader".