@@ -20,6 +20,7 @@ This repository contains the officially supported MongoDB Rust driver, a client
2020- [ Platforms] ( #platforms )
2121- [ Atlas note] ( #atlas-note )
2222- [ Windows DNS note] ( #windows-dns-note )
23+ - [ Warning about timeouts / cancellation] ( #warning-about-timeouts--cancellation )
2324- [ Bug Reporting / Feature Requests] ( #bug-reporting--feature-requests )
2425- [ Contributing] ( #contributing )
2526- [ Running the tests] ( #running-the-tests )
@@ -256,6 +257,30 @@ let options = ClientOptions::parse_with_resolver_config(
256257let client = Client :: with_options (options )? ;
257258```
258259
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+
259284## Bug Reporting / Feature Requests
260285To file a bug report or submit a feature request, please open a ticket on our [ Jira project] ( https://jira.mongodb.org/browse/RUST ) :
261286- Create an account and login at [ jira.mongodb.org] ( https://jira.mongodb.org )
0 commit comments