@@ -142,6 +142,10 @@ impl<'lua> Thread<'lua> {
142
142
A : IntoLuaMulti < ' lua > ,
143
143
R : FromLuaMulti < ' lua > ,
144
144
{
145
+ if self . status ( ) != ThreadStatus :: Resumable {
146
+ return Err ( Error :: CoroutineInactive ) ;
147
+ }
148
+
145
149
let lua = self . 0 . lua ;
146
150
let state = lua. state ( ) ;
147
151
let thread_state = self . state ( ) ;
@@ -165,10 +169,6 @@ impl<'lua> Thread<'lua> {
165
169
let state = lua. state ( ) ;
166
170
let thread_state = self . state ( ) ;
167
171
168
- if self . status ( ) != ThreadStatus :: Resumable {
169
- return Err ( Error :: CoroutineInactive ) ;
170
- }
171
-
172
172
let nargs = args. push_into_stack_multi ( lua) ?;
173
173
if nargs > 0 {
174
174
check_stack ( thread_state, nargs) ?;
@@ -196,6 +196,10 @@ impl<'lua> Thread<'lua> {
196
196
/// Gets the status of the thread.
197
197
pub fn status ( & self ) -> ThreadStatus {
198
198
let thread_state = self . state ( ) ;
199
+ if thread_state == self . 0 . lua . state ( ) {
200
+ // The coroutine is currently running
201
+ return ThreadStatus :: Unresumable ;
202
+ }
199
203
unsafe {
200
204
let status = ffi:: lua_status ( thread_state) ;
201
205
if status != ffi:: LUA_OK && status != ffi:: LUA_YIELD {
@@ -243,6 +247,9 @@ impl<'lua> Thread<'lua> {
243
247
pub fn reset ( & self , func : crate :: function:: Function < ' lua > ) -> Result < ( ) > {
244
248
let lua = self . 0 . lua ;
245
249
let thread_state = self . state ( ) ;
250
+ if thread_state == lua. state ( ) {
251
+ return Err ( Error :: runtime ( "cannot reset a running thread" ) ) ;
252
+ }
246
253
unsafe {
247
254
#[ cfg( all( feature = "lua54" , not( feature = "vendored" ) ) ) ]
248
255
let status = ffi:: lua_resetthread ( thread_state) ;
0 commit comments