@@ -20,6 +20,7 @@ This repository contains the officially supported MongoDB Rust driver, a client
20
20
- [ Platforms] ( #platforms )
21
21
- [ Atlas note] ( #atlas-note )
22
22
- [ Windows DNS note] ( #windows-dns-note )
23
+ - [ Warning about timeouts / cancellation] ( #warning-about-timeouts--cancellation )
23
24
- [ Bug Reporting / Feature Requests] ( #bug-reporting--feature-requests )
24
25
- [ Contributing] ( #contributing )
25
26
- [ Running the tests] ( #running-the-tests )
@@ -256,6 +257,30 @@ let options = ClientOptions::parse_with_resolver_config(
256
257
let client = Client :: with_options (options )? ;
257
258
```
258
259
260
+ ## Warning about timeouts / cancellation
261
+
262
+ In async Rust, it is common to implement cancellation and timeouts by dropping a future after a
263
+ certain period of time instead of polling it to completion. This is how
264
+ [ ` tokio::time::timeout ` ] ( https://docs.rs/tokio/1.10.1/tokio/time/fn.timeout.html ) works, for
265
+ example. However, doing this with futures returned by the driver can leave the driver's internals in
266
+ an inconsistent state, which may lead to unpredictable or incorrect behavior (see RUST-937 for more
267
+ details). As such, it is ** _ highly_ ** recommended to poll all futures returned from the driver to
268
+ completion. In order to still use timeout mechanisms like ` tokio::time::timeout ` with the driver,
269
+ one option is to spawn tasks and time out on their
270
+ [ ` JoinHandle ` ] ( https://docs.rs/tokio/1.10.1/tokio/task/struct.JoinHandle.html ) futures instead of on
271
+ the driver's futures directly. This will ensure the driver's futures will always be completely polled
272
+ while also allowing the application to continue in the event of a timeout.
273
+
274
+ e.g.
275
+ ``` rust
276
+ let collection = client . database (" ok" ). collection (" ok" );
277
+ let handle = tokio :: task :: spawn (async move {
278
+ collection . insert_one (doc! { " x" : 1 }, None ). await
279
+ });
280
+
281
+ tokio :: time :: timeout (Duration :: from_secs (5 ), handle ). await ??? ;
282
+ ```
283
+
259
284
## Bug Reporting / Feature Requests
260
285
To file a bug report or submit a feature request, please open a ticket on our [ Jira project] ( https://jira.mongodb.org/browse/RUST ) :
261
286
- Create an account and login at [ jira.mongodb.org] ( https://jira.mongodb.org )
0 commit comments