| @@ -15,8 +15,10 @@ |
| 15 | 15 | //! - **Cancellation.** When the outermost `tl_handle_ctl` finishes while |
| 16 | 16 | //! the fiber still waits for a `resume` that never came, the fiber is |
| 17 | 17 | //! resumed in *cancel* mode: its perform returns with `yielding = |
| 18 | | −//! CANCEL`, generated code unwinds it, and every reference it held is |
| 19 | | −//! dropped (exact Perceus). A `resume` called after that panics. |
| 18 | +//! CANCEL`, generated code unwinds it (running its `defer` cleanups), |
| 19 | +//! and every reference it held is dropped (exact Perceus). A `resume` |
| 20 | +//! called after that panics. If a cleanup panics (or the actor is |
| 21 | +//! killed meanwhile), that unwinding continues past the handler. |
| 20 | 22 | //! - **Preemption** inside a fiber is forwarded up to the actor (the |
| 21 | 23 | //! actor's own fiber yields to the scheduler), then back down. |
| 22 | 24 | //! |
| @@ -151,8 +153,11 @@ pub unsafe extern "C" fn tl_handle_ctl( |
| 151 | 153 | Block::fields_ptr(block as *mut Block) |
| 152 | 154 | .cast::<*mut Frame>() |
| 153 | 155 | .write(f); |
| 154 | | − let result = drive(ctx, block, Resume::Value(UNIT)); |
| 155 | | − cancel(ctx, block); |
| 156 | + let mut result = drive(ctx, block, Resume::Value(UNIT)); |
| 157 | + if cancel(ctx, block) { |
| 158 | + drop_value(ctx, result); |
| 159 | + result = UNIT; |
| 160 | + } |
| 156 | 161 | drop_value(ctx, block); |
| 157 | 162 | result |
| 158 | 163 | } |
| @@ -286,10 +291,12 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 286 | 291 | }; |
| 287 | 292 | if let Resume::Cancel = input { |
| 288 | 293 | // The fiber around us is being cancelled: |
| 289 | | − // cancel ours, then keep unwinding. |
| 294 | + // cancel ours, then keep unwinding (or |
| 295 | + // unwind as a cleanup of ours did). |
| 290 | 296 | (*ctx).yielding = YIELD_NONE; |
| 291 | | − cancel(ctx, block); |
| 292 | | − (*ctx).yielding = YIELD_CANCEL; |
| 297 | + if !cancel(ctx, block) { |
| 298 | + (*ctx).yielding = YIELD_CANCEL; |
| 299 | + } |
| 293 | 300 | return UNIT; |
| 294 | 301 | } |
| 295 | 302 | } |
| @@ -301,17 +308,19 @@ unsafe fn drive(ctx: *mut Ctx, block: Value, mut input: Resume) -> Value { |
| 301 | 308 | } |
| 302 | 309 | |
| 303 | 310 | /// If the frame's fiber is suspended, resume it in cancel mode so it |
| 304 | | −/// unwinds (dropping what it holds) and finishes. The unwinding state of |
| 305 | | −/// the caller is preserved. |
| 311 | +/// unwinds (dropping what it holds, running its cleanups) and finishes. |
| 312 | +/// The unwinding state of the caller is preserved, unless the cancelled |
| 313 | +/// code started an unwinding of its own (a cleanup panicked, or the actor |
| 314 | +/// was killed): that one replaces it, and this returns true. |
| 306 | 315 | /// |
| 307 | 316 | /// # Safety |
| 308 | 317 | /// `ctx` must be the running actor's context; `block` a live frame. |
| 309 | | −unsafe fn cancel(ctx: *mut Ctx, block: Value) { |
| 318 | +unsafe fn cancel(ctx: *mut Ctx, block: Value) -> bool { |
| 310 | 319 | // SAFETY: caller guarantees a live frame on this actor. |
| 311 | 320 | unsafe { |
| 312 | 321 | let f = frame(block); |
| 313 | 322 | let Some(mut fiber) = f.fiber.take() else { |
| 314 | | − return; |
| 323 | + return false; |
| 315 | 324 | }; |
| 316 | 325 | f.waiting = false; |
| 317 | 326 | f.cancelled = true; |
| @@ -347,9 +356,14 @@ unsafe fn cancel(ctx: *mut Ctx, block: Value) { |
| 347 | 356 | } |
| 348 | 357 | *current(ctx) = outer; |
| 349 | 358 | let c = &mut *ctx; |
| 359 | + if c.yielding != YIELD_NONE && c.yielding != YIELD_CANCEL { |
| 360 | + drop_value(ctx, saved.2); |
| 361 | + return true; |
| 362 | + } |
| 350 | 363 | drop_value(ctx, std::mem::replace(&mut c.yield_value, saved.2)); |
| 351 | 364 | c.yielding = saved.0; |
| 352 | 365 | c.yield_marker = saved.1; |
| 366 | + false |
| 353 | 367 | } |
| 354 | 368 | } |
| 355 | 369 | |