Impl Drop for Future generated by async fn
⚓ Rust 📅 2025-11-30 👤 surdeus 👁️ 4I understand async fn compiles to some implicit type T implements Future trait. I want to have a way to impl Drop for that implicit type T.
The more specific question:
I have an RPC service, every RPC request/response message has a transaction ID, the response RPC should contain the same ID as request RPC message.
Now, the async fn send_request generates a new ID stores that ID in a global hash map, then send RPC. The singleton looping handler async fn recv_response() of income RPCs may or may not receive the corresponding response, and during waiting for that response, unrelated response/requests may come in.
The recv handler will look up the ID of every response in the hash map, if matches, a oneshot channel is sent to notify the corresponding send_request fn, send_request fn will delete the ID in the hash map and returns the result.
Everything is fine except dropping the future. If I want to use a tokio timeout() or tokio select! macro, the future generated by send_request may be dropped while NOT DELETING the ID in hash map!
It would be easy if the ID is deleted in drop. Note this is a sync drop.
And I found it extremely difficult to manually write a Future and poll for this (e.g. fn send_request() -> SomeFuture). I have to store waker of that future everywhere, even in async fns. And calling functions originally designed for async fn but in poll() is difficult.
1 post - 1 participant
🏷️ Rust_feed