mlua-isle 0.10.0

Thread-isolated Lua VM with cancellation, async coroutines, and connection pool for mlua
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Typed errors (#11): `LuaFailure` on every path, cancellation as a
//! value, `task.is_cancelled`, and the protected call surviving a
//! sandboxed VM.
//!
//! `sync_isle` needs no feature; `runtime_paths` needs `tokio`.

use mlua_isle::{Isle, IsleError, LuaErrorKind, LuaFailure};

#[derive(Debug)]
struct MyErr(u32);

impl std::fmt::Display for MyErr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "my error {}", self.0)
    }
}

impl std::error::Error for MyErr {}

fn failure<T: std::fmt::Debug>(r: Result<T, IsleError>) -> LuaFailure {
    match r {
        Err(IsleError::Lua(f)) => f,
        other => panic!("expected IsleError::Lua, got: {other:?}"),
    }
}

/// Code that allocates until a 1 MiB memory limit is hit.
const ALLOC_LOOP: &str = "local t = {} for i = 1, 1e8 do t[i] = tostring(i) end";

/// Raises `E0`, then its `<close>` handler runs out of memory during the
/// unwind (a memory error does not go through the message handler).
const CLOSE_OOM: &str = "local c <close> = setmetatable({}, { __close = function()
  local t = {} for i = 1, 1e8 do t[i] = tostring(i) end
end })
error('E0')";

/// Replace the globals with a whitelist that has no `xpcall` and no
/// `table`.
fn sandbox(lua: &mlua::Lua) -> mlua::Result<()> {
    let g = lua.globals();
    let white = lua.create_table()?;
    for name in ["error", "setmetatable", "tostring"] {
        white.set(name, g.get::<mlua::Value>(name)?)?;
    }
    lua.set_globals(white)
}

mod sync_isle {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    #[test]
    fn sync_eval_survives_init_removing_xpcall() {
        let isle = Isle::spawn(|lua| lua.globals().set("xpcall", mlua::Value::Nil)).unwrap();
        assert_eq!(isle.eval::<i64>("return 1 + 1").unwrap(), 2);
        let f = failure(isle.eval::<()>("error({ code = 1 })"));
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert!(f.traceback.is_some());
        isle.shutdown().unwrap();
    }

    #[test]
    fn sync_eval_survives_whitelisted_globals() {
        let isle = Isle::spawn(sandbox).unwrap();
        assert_eq!(isle.eval::<i64>("return 40 + 2").unwrap(), 42);
        let f = failure(isle.eval::<()>("error('x')"));
        assert_eq!(f.message, "eval:1: x");
        assert!(f.traceback.is_some(), "the message handler did not run");
        isle.shutdown().unwrap();
    }

    #[test]
    fn sync_memory_error_is_memory() {
        let isle = Isle::spawn(|lua| lua.set_memory_limit(1 << 20).map(drop)).unwrap();
        let f = failure(isle.eval::<()>(ALLOC_LOOP));
        assert_eq!(f.kind, LuaErrorKind::Memory, "got: {f:?}");
        assert_eq!(f.traceback, None);
        // The VM still serves.
        assert_eq!(isle.eval::<i64>("return 1").unwrap(), 1);
        isle.shutdown().unwrap();
    }

    #[test]
    fn a_failing_line_hook_does_not_mask_the_lua_error() {
        // The hook fails at the first line event after `arm()`.  The
        // message handler runs no Lua, so that event does not come
        // before the error reaches Rust.
        let armed = Arc::new(AtomicBool::new(false));
        let a = armed.clone();
        let isle = Isle::spawn(move |lua| {
            let h = armed.clone();
            mlua_isle::runtime::Vm::attach(lua, Default::default())
                .and_then(|vm| {
                    vm.add_hook(mlua::HookTriggers::new().every_line(), move |_, _| {
                        if h.swap(false, Ordering::SeqCst) {
                            return Err(mlua::Error::runtime("hook failed"));
                        }
                        Ok(mlua::VmState::Continue)
                    })
                })
                .map_err(|e| mlua::Error::runtime(e.to_string()))?;
            let arm = lua.create_function(move |_, ()| {
                a.store(true, Ordering::SeqCst);
                Ok(())
            })?;
            lua.globals().set("arm", arm)
        })
        .unwrap();
        let f = failure(isle.eval::<()>("arm() error('original')"));
        assert_eq!(f.message, "eval:1: original");
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        isle.shutdown().unwrap();
    }

    #[test]
    fn sync_memory_error_in_close_after_an_error_is_memory() {
        let isle = Isle::spawn(|lua| lua.set_memory_limit(1 << 20).map(drop)).unwrap();
        let f = failure(isle.eval::<()>(CLOSE_OOM));
        assert_eq!(f.kind, LuaErrorKind::Memory, "got: {f:?}");
        assert_eq!(f.traceback, None);
        assert_eq!(isle.eval::<i64>("return 1").unwrap(), 1);
        isle.shutdown().unwrap();
    }

    #[test]
    fn attach_without_xpcall_is_an_init_error() {
        let lua = mlua::Lua::new();
        lua.globals().set("xpcall", mlua::Value::Nil).unwrap();
        let err = mlua_isle::runtime::Vm::attach(&lua, mlua_isle::runtime::Config::default())
            .unwrap_err();
        assert!(
            matches!(&err, IsleError::Init(f) if f.kind == LuaErrorKind::External),
            "got: {err:?}"
        );
    }

    #[test]
    fn a_cyclic_table_has_no_value_and_keeps_its_message() {
        let isle = Isle::spawn(|_| Ok(())).unwrap();
        let f = failure(isle.eval::<()>(
            "local t = setmetatable({}, { __tostring = function() return 'cyclic' end })
             t.self = t
             error(t)",
        ));
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert_eq!(f.message, "cyclic");
        assert!(f.traceback.is_some());
        #[cfg(feature = "serde")]
        assert_eq!(f.value, None);
        isle.shutdown().unwrap();
    }

    #[test]
    fn a_host_error_through_isle_is_a_callback_failure() {
        let isle = Isle::spawn(|lua| {
            let fail = lua.create_function(|_, ()| -> mlua::Result<()> {
                Err(mlua::Error::external(MyErr(3)))
            })?;
            lua.globals().set("fail", fail)
        })
        .unwrap();
        let f = failure(isle.eval::<()>("fail()"));
        assert_eq!(f.kind, LuaErrorKind::Callback);
        assert_eq!(f.message, "my error 3");
        isle.shutdown().unwrap();
    }

    #[test]
    fn isle_error_display() {
        let f = LuaFailure::new(LuaErrorKind::Runtime, "boom");
        assert_eq!(IsleError::Lua(f.clone()).to_string(), "lua error: boom");
        assert_eq!(IsleError::Init(f).to_string(), "init error: boom");
        assert_eq!(
            IsleError::NotFound("f".into()).to_string(),
            "function 'f' not found"
        );
        assert_eq!(
            IsleError::ThreadPanic(Some("x".into())).to_string(),
            "lua thread panicked: x"
        );
        assert_eq!(
            IsleError::ThreadPanic(None).to_string(),
            "lua thread panicked"
        );
        assert_eq!(IsleError::RecvFailed.to_string(), "recv failed");
    }
}

#[cfg(feature = "tokio")]
mod runtime_paths {
    use super::*;
    use mlua_isle::runtime::cancellable;
    use mlua_isle::runtime::{CancelToken, Config, Vm};
    use mlua_isle::AsyncIsle;
    use std::cell::RefCell;
    use std::rc::Rc;
    use std::time::Duration;

    /// A VM driven in this thread: `task`, `sleep(ms)` (cancellable),
    /// `fail()` (a host error) and `report(...)` (records its arguments as
    /// strings).
    struct Local {
        rt: tokio::runtime::Runtime,
        local: tokio::task::LocalSet,
        lua: mlua::Lua,
        vm: Vm,
        reports: Rc<RefCell<Vec<Vec<String>>>>,
    }

    impl Local {
        fn new() -> Self {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();
            let local = tokio::task::LocalSet::new();
            let lua = mlua::Lua::new();
            let vm = Vm::attach(
                &lua,
                Config {
                    grace: Duration::from_millis(500),
                    ..Default::default()
                },
            )
            .unwrap();
            let g = lua.globals();
            g.set("task", vm.task_lib().unwrap()).unwrap();
            let sleep = lua
                .create_async_function(|_, ms: u64| {
                    cancellable(async move {
                        tokio::time::sleep(Duration::from_millis(ms)).await;
                        Ok(())
                    })
                })
                .unwrap();
            g.set("sleep", sleep).unwrap();
            let fail = lua
                .create_function(|_, ()| -> mlua::Result<()> {
                    Err(mlua::Error::external(MyErr(7)))
                })
                .unwrap();
            g.set("fail", fail).unwrap();
            let reports: Rc<RefCell<Vec<Vec<String>>>> = Default::default();
            let r = reports.clone();
            let report = lua
                .create_function(move |_, args: mlua::Variadic<mlua::Value>| {
                    let row = args
                        .iter()
                        .map(|v| v.to_string().unwrap_or_else(|_| format!("{v:?}")))
                        .collect();
                    r.borrow_mut().push(row);
                    Ok(())
                })
                .unwrap();
            g.set("report", report).unwrap();
            Self {
                rt,
                local,
                lua,
                vm,
                reports,
            }
        }

        /// Run `code` as a root; cancel the token after `cancel_after`.
        fn run(
            &self,
            code: &str,
            cancel_after: Option<Duration>,
        ) -> Result<mlua::MultiValue, IsleError> {
            let f = self.lua.load(code).into_function()?;
            let token = CancelToken::new();
            if let Some(d) = cancel_after {
                // From another thread: a CPU loop never yields to this one.
                let t = token.clone();
                std::thread::spawn(move || {
                    std::thread::sleep(d);
                    t.cancel();
                });
            }
            self.local.block_on(&self.rt, self.vm.run(&token, f, ()))
        }

        fn reports(&self) -> Vec<Vec<String>> {
            self.reports.borrow().clone()
        }
    }

    // ── Vm::run ───────────────────────────────────────────────────────

    #[test]
    fn run_of_a_raised_table_keeps_its_message_and_value() {
        let l = Local::new();
        let f = failure(l.run("local t = { code = 42 } shown = tostring(t) error(t)", None));
        let shown: String = l.lua.globals().get("shown").unwrap();
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert_eq!(f.message, shown);
        assert!(
            f.traceback
                .as_deref()
                .unwrap_or("")
                .contains("stack traceback"),
            "traceback: {:?}",
            f.traceback
        );
        #[cfg(feature = "serde")]
        assert_eq!(f.value.unwrap()["code"], 42);
    }

    #[test]
    fn run_of_a_table_with_tostring_uses_it() {
        let l = Local::new();
        let f = failure(l.run(
            "error(setmetatable({ code = 7 }, { __tostring = function() return 'custom E7' end }))",
            None,
        ));
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert_eq!(f.message, "custom E7");
        #[cfg(feature = "serde")]
        assert_eq!(f.value.unwrap()["code"], 7);
    }

    #[test]
    fn run_of_an_unconvertible_value_has_no_value() {
        let l = Local::new();
        let f = failure(l.run("error(function() end)", None));
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert!(f.message.starts_with("function: "), "got: {}", f.message);
        #[cfg(feature = "serde")]
        assert_eq!(f.value, None);
    }

    #[test]
    fn a_chunk_with_a_syntax_error_is_syntax() {
        let l = Local::new();
        let f = failure(l.run("x = = 1", None));
        assert_eq!(f.kind, LuaErrorKind::Syntax);
    }

    #[test]
    fn a_host_error_is_a_callback_failure_with_its_message() {
        let l = Local::new();
        let f = failure(l.run("fail()", None));
        assert_eq!(f.kind, LuaErrorKind::Callback);
        assert_eq!(f.message, "my error 7");
        assert!(f.traceback.is_some());
        #[cfg(feature = "serde")]
        assert_eq!(f.value, None);
    }

    #[test]
    fn a_cancelled_root_is_cancelled() {
        let l = Local::new();
        let r = l.run("sleep(5000)", Some(Duration::from_millis(20)));
        assert!(matches!(r, Err(IsleError::Cancelled)), "got: {r:?}");
    }

    #[test]
    fn a_cancelled_cpu_loop_root_is_cancelled() {
        let l = Local::new();
        let r = l.run("while true do end", Some(Duration::from_millis(20)));
        assert!(matches!(r, Err(IsleError::Cancelled)), "got: {r:?}");
    }

    #[test]
    fn an_error_with_the_old_sentinel_is_not_a_cancel() {
        let l = Local::new();
        let f = failure(l.run("error('__isle_cancelled__')", None));
        assert!(
            f.message.ends_with("__isle_cancelled__"),
            "got: {}",
            f.message
        );
    }

    #[test]
    fn a_host_that_raises_cancelled_is_recognised_by_value() {
        let l = Local::new();
        let raise = l
            .lua
            .create_function(|_, ()| -> mlua::Result<()> {
                Err(mlua::Error::external(mlua_isle::Cancelled))
            })
            .unwrap();
        l.lua.globals().set("raise_cancel", raise).unwrap();
        let r = l.run("raise_cancel()", None);
        assert!(matches!(r, Err(IsleError::Cancelled)), "got: {r:?}");
        // And Lua sees it as a cancel too.
        let out = l
            .run(
                "local ok, e = pcall(raise_cancel) return task.is_cancelled(e)",
                None,
            )
            .unwrap();
        assert_eq!(out[0].as_boolean(), Some(true));
    }

    // ── task.is_cancelled ────────────────────────────────────────────────

    #[test]
    fn pcall_under_a_cancel_gives_a_cancel_value() {
        let l = Local::new();
        let r = l.run(
            "local ok, err = pcall(sleep, 5000)
             report(ok, task.is_cancelled(err))
             local ok2, err2 = pcall(function() while true do end end)
             report(ok2, task.is_cancelled(err2))",
            Some(Duration::from_millis(20)),
        );
        assert!(matches!(r, Err(IsleError::Cancelled)), "got: {r:?}");
        assert_eq!(
            l.reports(),
            vec![
                vec!["false".to_string(), "true".to_string()],
                vec!["false".to_string(), "true".to_string()],
            ]
        );
    }

    #[test]
    fn a_close_handler_receives_a_cancel_value() {
        let l = Local::new();
        let r = l.run(
            "local c <close> = setmetatable({}, { __close = function(_, e)
                 report(e ~= nil, task.is_cancelled(e))
             end })
             sleep(5000)",
            Some(Duration::from_millis(20)),
        );
        assert!(matches!(r, Err(IsleError::Cancelled)), "got: {r:?}");
        assert_eq!(
            l.reports(),
            vec![vec!["true".to_string(), "true".to_string()]]
        );
    }

    #[test]
    fn join_of_a_cancelled_task_still_returns_task_cancelled() {
        let l = Local::new();
        let out = l
            .run(
                "local h = task.spawn(function() sleep(5000) end)
                 sleep(5)
                 h:cancel()
                 local ok, e = h:join()
                 local _, host = pcall(fail)
                 return ok, rawequal(e, task.CANCELLED), task.is_cancelled(e),
                        task.is_cancelled(task.CANCELLED),
                        task.is_cancelled('some string'), task.is_cancelled(nil),
                        task.is_cancelled({}), task.is_cancelled(host)",
                None,
            )
            .unwrap();
        let got: Vec<Option<bool>> = out.iter().map(|v| v.as_boolean()).collect();
        assert_eq!(
            got,
            vec![
                Some(false),
                Some(true),
                Some(true),
                Some(true),
                Some(false),
                Some(false),
                Some(false),
                Some(false),
            ]
        );
    }

    #[test]
    fn a_task_error_value_is_unchanged() {
        let l = Local::new();
        let out = l
            .run(
                "local h = task.spawn(function() error({ code = 42 }) end)
                 local ok, e = h:join()
                 return ok, e.code",
                None,
            )
            .unwrap();
        assert_eq!(out[0].as_boolean(), Some(false));
        assert_eq!(out[1].as_i64(), Some(42));
    }

    // ── the same LuaFailure through the actors ───────────────────────────

    const RAISE: &str =
        "error(setmetatable({ code = 42 }, { __tostring = function() return 'E42' end }))";

    fn assert_e42(f: &LuaFailure) {
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert_eq!(f.message, "E42");
        assert!(f.traceback.is_some());
        #[cfg(feature = "serde")]
        assert_eq!(f.value.as_ref().unwrap()["code"], 42);
    }

    #[tokio::test]
    async fn async_isle_requests_carry_the_same_failure() {
        let (isle, driver) =
            AsyncIsle::spawn(|lua| lua.load(format!("function raise() {RAISE} end")).exec())
                .await
                .unwrap();

        assert_e42(&failure(isle.eval::<()>(RAISE).await));
        assert_e42(&failure(isle.coroutine_eval::<()>(RAISE).await));
        assert_e42(&failure(isle.call::<_, ()>("raise", ()).await));
        assert_e42(&failure(isle.coroutine_call::<_, ()>("raise", ()).await));
        assert_e42(&failure(isle.spawn_coroutine_eval::<()>(RAISE).await));

        let sync = Isle::spawn(|_| Ok(())).unwrap();
        assert_e42(&failure(sync.eval::<()>(RAISE)));
        sync.shutdown().unwrap();

        driver.shutdown().await.unwrap();
    }

    #[test]
    fn vm_run_carries_the_same_failure_as_the_actors() {
        let l = Local::new();
        assert_e42(&failure(l.run(RAISE, None)));
    }

    #[tokio::test]
    async fn async_isle_not_found_and_old_sentinel() {
        let (isle, driver) = AsyncIsle::spawn(|_| Ok(())).await.unwrap();
        assert!(
            matches!(isle.call::<_, ()>("nope", ()).await, Err(IsleError::NotFound(n)) if n == "nope")
        );
        assert!(matches!(
            isle.coroutine_call::<_, ()>("nope", ()).await,
            Err(IsleError::NotFound(n)) if n == "nope"
        ));
        let f = failure(
            isle.coroutine_eval::<()>("error('__isle_cancelled__')")
                .await,
        );
        assert!(f.message.ends_with("__isle_cancelled__"));
        let f = failure(isle.eval::<()>("error('__isle_cancelled__')").await);
        assert!(f.message.ends_with("__isle_cancelled__"));
        driver.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn async_isle_init_errors() {
        let err = AsyncIsle::spawn(|_| Err(mlua::Error::runtime("nope")))
            .await
            .err()
            .expect("init must fail");
        assert!(
            matches!(&err, IsleError::Init(f) if f.kind == LuaErrorKind::Runtime && f.message == "nope"),
            "got: {err:?}"
        );
        let err = AsyncIsle::spawn(|_| panic!("async init boom"))
            .await
            .err()
            .expect("init must fail");
        assert!(
            matches!(&err, IsleError::ThreadPanic(Some(m)) if m == "async init boom"),
            "got: {err:?}"
        );
    }

    // ── the protected call on a sandboxed VM ────────────────────────────

    #[tokio::test]
    async fn coroutine_eval_survives_init_removing_xpcall() {
        let (isle, driver) = AsyncIsle::spawn(|lua| lua.globals().set("xpcall", mlua::Value::Nil))
            .await
            .unwrap();
        assert_eq!(isle.coroutine_eval::<i64>("return 1 + 1").await.unwrap(), 2);
        assert_eq!(isle.eval::<i64>("return 2 + 2").await.unwrap(), 4);
        let f = failure(isle.coroutine_eval::<()>("error({ code = 1 })").await);
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert!(f.traceback.is_some());
        driver.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn coroutine_eval_survives_whitelisted_globals() {
        let (isle, driver) = AsyncIsle::spawn(sandbox).await.unwrap();
        assert_eq!(
            isle.coroutine_eval::<i64>("return 40 + 2").await.unwrap(),
            42
        );
        let f = failure(isle.coroutine_eval::<()>("error('x')").await);
        assert_eq!(f.message, "coroutine_eval:1: x");
        assert!(f.traceback.is_some(), "the message handler did not run");
        driver.shutdown().await.unwrap();
    }

    #[test]
    fn vm_run_survives_removing_xpcall_and_whitelisted_globals() {
        let l = Local::new();
        l.lua.globals().set("xpcall", mlua::Value::Nil).unwrap();
        assert_eq!(l.run("return 1 + 1", None).unwrap()[0].as_i64(), Some(2));
        sandbox(&l.lua).unwrap();
        assert_eq!(l.run("return 40 + 2", None).unwrap()[0].as_i64(), Some(42));
        let f = failure(l.run("error({ code = 1 })", None));
        assert_eq!(f.kind, LuaErrorKind::Runtime);
        assert!(f.traceback.is_some(), "the message handler did not run");
        // A re-attach after the sandbox keeps the captured `xpcall`.
        Vm::attach(&l.lua, Config::default()).unwrap();
        assert_eq!(l.run("return 3", None).unwrap()[0].as_i64(), Some(3));
    }

    #[tokio::test]
    async fn coroutine_memory_error_is_memory() {
        let (isle, driver) = AsyncIsle::spawn(|lua| lua.set_memory_limit(1 << 20).map(drop))
            .await
            .unwrap();
        let f = failure(isle.coroutine_eval::<()>(ALLOC_LOOP).await);
        assert_eq!(f.kind, LuaErrorKind::Memory, "got: {f:?}");
        let f = failure(isle.eval::<()>(ALLOC_LOOP).await);
        assert_eq!(f.kind, LuaErrorKind::Memory, "got: {f:?}");
        assert_eq!(isle.coroutine_eval::<i64>("return 1").await.unwrap(), 1);
        driver.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn coroutine_memory_error_in_close_after_an_error_is_memory() {
        let (isle, driver) = AsyncIsle::spawn(|lua| lua.set_memory_limit(1 << 20).map(drop))
            .await
            .unwrap();
        let f = failure(isle.coroutine_eval::<()>(CLOSE_OOM).await);
        assert_eq!(f.kind, LuaErrorKind::Memory, "got: {f:?}");
        assert_eq!(f.traceback, None);
        assert_eq!(isle.coroutine_eval::<i64>("return 1").await.unwrap(), 1);
        driver.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn interleaved_failing_roots_keep_their_own_traceback() {
        let (isle, driver) = AsyncIsle::spawn(|lua| {
            let sleep = lua.create_async_function(|_, ms: u64| async move {
                tokio::time::sleep(Duration::from_millis(ms)).await;
                Ok(())
            })?;
            lua.globals().set("sleep", sleep)
        })
        .await
        .unwrap();
        // A fails at once and its `<close>` handler then awaits 50 ms;
        // B fails at 10 ms, while A is between its message handler and
        // the return of its `xpcall`.
        let a = "local c <close> = setmetatable({}, { __close = function() sleep(50) end })
error('A')";
        let b = "sleep(10)
sleep(0)
error('B')";
        let (ra, rb) = tokio::join!(isle.coroutine_eval::<()>(a), isle.coroutine_eval::<()>(b));
        let (fa, fb) = (failure(ra), failure(rb));
        assert_eq!(fa.message, "coroutine_eval:2: A");
        assert_eq!(fb.message, "coroutine_eval:3: B");
        let (ta, tb) = (fa.traceback.unwrap(), fb.traceback.unwrap());
        assert!(ta.contains("coroutine_eval:2:"), "A: {ta}");
        assert!(!ta.contains("coroutine_eval:3:"), "A: {ta}");
        assert!(tb.contains("coroutine_eval:3:"), "B: {tb}");
        assert!(!tb.contains("coroutine_eval:2:"), "B: {tb}");
        driver.shutdown().await.unwrap();
    }
}