Skip to content

Commit ac2c632

Browse files
committed
Move check for previous value of "closed" outside of lambda (#1058)
JAVA-4821
1 parent 73dca7d commit ac2c632

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

driver-sync/src/main/com/mongodb/client/gridfs/GridFSUploadStreamImpl.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ public void write(final byte[] b, final int off, final int len) {
138138

139139
@Override
140140
public void close() {
141-
withLock(closeLock, () -> {
142-
if (closed) {
143-
return;
144-
}
141+
boolean alreadyClosed = withLock(closeLock, () -> {
142+
boolean prevClosed = closed;
145143
closed = true;
144+
return prevClosed;
146145
});
146+
if (alreadyClosed) {
147+
return;
148+
}
147149
writeChunk();
148150
GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(),
149151
metadata);

driver-sync/src/test/functional/com/mongodb/client/gridfs/GridFSBucketSmokeTestSpecification.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
8484
def outputStream = gridFSBucket.openUploadStream('myFile')
8585
outputStream.write(contentBytes)
8686
outputStream.close()
87+
outputStream.close() // check for close idempotency
8788
fileId = outputStream.getObjectId()
8889
}
8990

0 commit comments

Comments
 (0)