Skip to content

Commit 8b4776f

Browse files
authored
Handle interrupts: synchronous KeyManagementService uses Socket IO (open, read, write), which is interruptible in a virtual thread (#1204)
JAVA-5139
1 parent b9f70ad commit 8b4776f

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

driver-sync/src/main/com/mongodb/client/internal/Crypt.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import static com.mongodb.assertions.Assertions.notNull;
4949
import static com.mongodb.crypt.capi.MongoCryptContext.State;
5050
import static com.mongodb.internal.client.vault.EncryptOptionsHelper.asMongoExplicitEncryptOptions;
51+
import static com.mongodb.internal.thread.InterruptionUtil.translateInterruptedException;
5152

5253
/**
5354
* <p>This class is not part of the public API and may be removed or changed at any time</p>
@@ -141,7 +142,7 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
141142
try (MongoCryptContext encryptionContext = mongoCrypt.createEncryptionContext(databaseName, command)) {
142143
return executeStateMachine(encryptionContext, databaseName);
143144
} catch (MongoCryptException e) {
144-
throw wrapInClientException(e);
145+
throw wrapInMongoException(e);
145146
}
146147
}
147148

@@ -156,7 +157,7 @@ RawBsonDocument decrypt(final RawBsonDocument commandResponse) {
156157
try (MongoCryptContext decryptionContext = mongoCrypt.createDecryptionContext(commandResponse)) {
157158
return executeStateMachine(decryptionContext, null);
158159
} catch (MongoCryptException e) {
159-
throw wrapInClientException(e);
160+
throw wrapInMongoException(e);
160161
}
161162
}
162163

@@ -179,7 +180,7 @@ BsonDocument createDataKey(final String kmsProvider, final DataKeyOptions option
179180
.build())) {
180181
return executeStateMachine(dataKeyCreationContext, null);
181182
} catch (MongoCryptException e) {
182-
throw wrapInClientException(e);
183+
throw wrapInMongoException(e);
183184
}
184185
}
185186

@@ -198,7 +199,7 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
198199
new BsonDocument("v", value), asMongoExplicitEncryptOptions(options))) {
199200
return executeStateMachine(encryptionContext, null).getBinary("v");
200201
} catch (MongoCryptException e) {
201-
throw wrapInClientException(e);
202+
throw wrapInMongoException(e);
202203
}
203204
}
204205

@@ -218,7 +219,7 @@ BsonDocument encryptExpression(final BsonDocument expression, final EncryptOptio
218219
new BsonDocument("v", expression), asMongoExplicitEncryptOptions(options))) {
219220
return executeStateMachine(encryptionContext, null).getDocument("v");
220221
} catch (MongoCryptException e) {
221-
throw wrapInClientException(e);
222+
throw wrapInMongoException(e);
222223
}
223224
}
224225

@@ -233,7 +234,7 @@ BsonValue decryptExplicitly(final BsonBinary value) {
233234
try (MongoCryptContext decryptionContext = mongoCrypt.createExplicitDecryptionContext(new BsonDocument("v", value))) {
234235
return assertNotNull(executeStateMachine(decryptionContext, null).get("v"));
235236
} catch (MongoCryptException e) {
236-
throw wrapInClientException(e);
237+
throw wrapInMongoException(e);
237238
}
238239
}
239240

@@ -256,7 +257,7 @@ BsonDocument rewrapManyDataKey(final BsonDocument filter, final RewrapManyDataKe
256257
return executeStateMachine(rewrapManyDatakeyContext, null);
257258
}
258259
} catch (MongoCryptException e) {
259-
throw wrapInClientException(e);
260+
throw wrapInMongoException(e);
260261
}
261262
}
262263

@@ -324,7 +325,7 @@ private void mark(final MongoCryptContext cryptContext, final String databaseNam
324325
cryptContext.addMongoOperationResult(markedCommand);
325326
cryptContext.completeMongoOperation();
326327
} catch (Throwable t) {
327-
throw wrapInClientException(t);
328+
throw wrapInMongoException(t);
328329
}
329330
}
330331

@@ -348,7 +349,8 @@ private void decryptKeys(final MongoCryptContext cryptContext) {
348349
}
349350
cryptContext.completeKeyDecryptors();
350351
} catch (Throwable t) {
351-
throw wrapInClientException(t);
352+
throw translateInterruptedException(t, "Interrupted while doing IO")
353+
.orElseThrow(() -> wrapInMongoException(t));
352354
}
353355
}
354356

@@ -366,7 +368,11 @@ private void decryptKey(final MongoKeyDecryptor keyDecryptor) throws IOException
366368
}
367369
}
368370

369-
private MongoClientException wrapInClientException(final Throwable t) {
370-
return new MongoClientException("Exception in encryption library: " + t.getMessage(), t);
371+
private MongoException wrapInMongoException(final Throwable t) {
372+
if (t instanceof MongoException) {
373+
return (MongoException) t;
374+
} else {
375+
return new MongoClientException("Exception in encryption library: " + t.getMessage(), t);
376+
}
371377
}
372378
}

driver-sync/src/main/com/mongodb/client/internal/KeyManagementService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void enableHostNameVerification(final SSLSocket socket) {
9898
private void closeSocket(final Socket socket) {
9999
try {
100100
socket.close();
101-
} catch (IOException e) {
101+
} catch (IOException | RuntimeException e) {
102102
// ignore
103103
}
104104
}

0 commit comments

Comments
 (0)