@@ -5,6 +5,7 @@ use crate::io::uring::rename::Rename;
55use crate :: io:: uring:: utils:: ArcFd ;
66use crate :: io:: uring:: write:: Write ;
77
8+ use crate :: runtime:: io:: RegisterResult ;
89use crate :: runtime:: Handle ;
910
1011#[ cfg(
@@ -152,6 +153,10 @@ pub(crate) trait Completable {
152153 // The `Op` type that implements this trait can return the passed error
153154 // upstream by embedding it in the `Output`.
154155 fn complete_with_error ( self , error : io:: Error ) -> Self :: Output ;
156+
157+ // Like `complete_with_error`, but for an op cancelled after the SQE reached
158+ // the kernel: its resources stay with the driver and are not returned.
159+ fn complete_after_cancel ( error : io:: Error ) -> Self :: Output ;
155160}
156161
157162/// Extracts the `CancelData` needed to safely cancel an in-flight io_uring operation.
@@ -166,18 +171,22 @@ impl<T: Cancellable + Completable + Send> Future for Op<T> {
166171
167172 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
168173 let this = self . get_mut ( ) ;
169- let handle = & mut this. handle ;
170- let driver = handle. inner . driver ( ) . io ( ) ;
171174
172175 match & mut this. state {
173176 State :: Initialize ( entry_opt) => {
174177 let entry = entry_opt. take ( ) . expect ( "Entry must be present" ) ;
175178 let waker = cx. waker ( ) . clone ( ) ;
176179
180+ let driver = this. handle . inner . driver ( ) . io ( ) ;
177181 // SAFETY: entry is valid for the entire duration of the operation
178- match unsafe { driver. register_op ( entry, waker) } {
179- Ok ( idx) => this. state = State :: Polled ( idx) ,
180- Err ( err) => {
182+ let result = unsafe { driver. register_op ( entry, waker) } ;
183+
184+ match result {
185+ RegisterResult :: Submitted ( idx) => this. state = State :: Polled ( idx) ,
186+
187+ // The kernel never saw the entry, so the resources go back to
188+ // the caller with the error.
189+ RegisterResult :: NotPushed ( err) => {
181190 let data = this
182191 . take_data ( )
183192 . expect ( "Data must be present on Initialization" ) ;
@@ -186,12 +195,27 @@ impl<T: Cancellable + Completable + Send> Future for Op<T> {
186195
187196 return Poll :: Ready ( data. complete_with_error ( err) ) ;
188197 }
198+
199+ // The kernel may have seen the entry, so cancel to keep the
200+ // resources alive until completion instead of returning them.
201+ RegisterResult :: Pushed { index, error } => {
202+ let data = this
203+ . take_data ( )
204+ . expect ( "Data must be present on Initialization" ) ;
205+
206+ this. handle . inner . driver ( ) . io ( ) . cancel_op ( index, Some ( data) ) ;
207+
208+ this. state = State :: Complete ;
209+
210+ return Poll :: Ready ( T :: complete_after_cancel ( error) ) ;
211+ }
189212 } ;
190213
191214 Poll :: Pending
192215 }
193216
194217 State :: Polled ( idx) => {
218+ let driver = this. handle . inner . driver ( ) . io ( ) ;
195219 let mut ctx = driver. get_uring ( ) . lock ( ) ;
196220 let lifecycle = ctx. ops . get_mut ( * idx) . expect ( "Lifecycle must be present" ) ;
197221
0 commit comments