File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -1262,7 +1262,7 @@ impl RawLua {
1262
1262
if nargs == 1 && ffi:: lua_tolightuserdata ( state, -1 ) == Lua :: poll_terminate ( ) . 0 {
1263
1263
// Destroy the future and terminate the Lua thread
1264
1264
( * upvalue) . data . take ( ) ;
1265
- ffi:: lua_pushinteger ( state, 0 ) ;
1265
+ ffi:: lua_pushinteger ( state, - 1 ) ;
1266
1266
return Ok ( 1 ) ;
1267
1267
}
1268
1268
@@ -1347,6 +1347,10 @@ impl RawLua {
1347
1347
return res
1348
1348
elseif nres == 2 then
1349
1349
return res, res2
1350
+ elseif nres < 0 then
1351
+ -- Negative `nres` means that the future is terminated
1352
+ -- It must stay yielded and never be resumed again
1353
+ yield()
1350
1354
else
1351
1355
return unpack(res, nres)
1352
1356
end
Original file line number Diff line number Diff line change @@ -529,6 +529,7 @@ impl<R> Drop for AsyncThread<R> {
529
529
// The thread is dropped while yielded, resume it with the "terminate" signal
530
530
ffi:: lua_pushlightuserdata ( self . thread . 1 , crate :: Lua :: poll_terminate ( ) . 0 ) ;
531
531
if let Ok ( ( new_status, _) ) = self . thread . resume_inner ( & lua, 1 ) {
532
+ // `new_status` should always be `ThreadStatusInner::Yielded(0)`
532
533
status = new_status;
533
534
}
534
535
}
Original file line number Diff line number Diff line change @@ -610,6 +610,36 @@ async fn test_async_task() -> Result<()> {
610
610
Ok ( ( ) )
611
611
}
612
612
613
+ #[ tokio:: test]
614
+ async fn test_async_task_abort ( ) -> Result < ( ) > {
615
+ let lua = Lua :: new ( ) ;
616
+
617
+ let sleep = lua. create_async_function ( move |_lua, n : u64 | async move {
618
+ sleep_ms ( n) . await ;
619
+ Ok ( ( ) )
620
+ } ) ?;
621
+ lua. globals ( ) . set ( "sleep" , sleep) ?;
622
+
623
+ let local = tokio:: task:: LocalSet :: new ( ) ;
624
+ local
625
+ . run_until ( async {
626
+ let lua2 = lua. clone ( ) ;
627
+ let jh = tokio:: task:: spawn_local ( async move {
628
+ lua2. load ( "sleep(200) result = 'done'" )
629
+ . exec_async ( )
630
+ . await
631
+ . unwrap ( ) ;
632
+ } ) ;
633
+ sleep_ms ( 100 ) . await ; // Wait for the task to start
634
+ jh. abort ( ) ;
635
+ } )
636
+ . await ;
637
+ local. await ;
638
+ assert_eq ! ( lua. globals( ) . get:: <Value >( "result" ) ?, Value :: Nil ) ;
639
+
640
+ Ok ( ( ) )
641
+ }
642
+
613
643
#[ tokio:: test]
614
644
#[ cfg( not( feature = "luau" ) ) ]
615
645
async fn test_async_hook ( ) -> Result < ( ) > {
You can’t perform that action at this time.
0 commit comments