@@ -142,6 +142,10 @@ impl<'lua> Thread<'lua> {
142142 A : IntoLuaMulti < ' lua > ,
143143 R : FromLuaMulti < ' lua > ,
144144 {
145+ if self . status ( ) != ThreadStatus :: Resumable {
146+ return Err ( Error :: CoroutineInactive ) ;
147+ }
148+
145149 let lua = self . 0 . lua ;
146150 let state = lua. state ( ) ;
147151 let thread_state = self . state ( ) ;
@@ -165,10 +169,6 @@ impl<'lua> Thread<'lua> {
165169 let state = lua. state ( ) ;
166170 let thread_state = self . state ( ) ;
167171
168- if self . status ( ) != ThreadStatus :: Resumable {
169- return Err ( Error :: CoroutineInactive ) ;
170- }
171-
172172 let nargs = args. push_into_stack_multi ( lua) ?;
173173 if nargs > 0 {
174174 check_stack ( thread_state, nargs) ?;
@@ -196,6 +196,10 @@ impl<'lua> Thread<'lua> {
196196 /// Gets the status of the thread.
197197 pub fn status ( & self ) -> ThreadStatus {
198198 let thread_state = self . state ( ) ;
199+ if thread_state == self . 0 . lua . state ( ) {
200+ // The coroutine is currently running
201+ return ThreadStatus :: Unresumable ;
202+ }
199203 unsafe {
200204 let status = ffi:: lua_status ( thread_state) ;
201205 if status != ffi:: LUA_OK && status != ffi:: LUA_YIELD {
@@ -243,6 +247,9 @@ impl<'lua> Thread<'lua> {
243247 pub fn reset ( & self , func : crate :: function:: Function < ' lua > ) -> Result < ( ) > {
244248 let lua = self . 0 . lua ;
245249 let thread_state = self . state ( ) ;
250+ if thread_state == lua. state ( ) {
251+ return Err ( Error :: runtime ( "cannot reset a running thread" ) ) ;
252+ }
246253 unsafe {
247254 #[ cfg( all( feature = "lua54" , not( feature = "vendored" ) ) ) ]
248255 let status = ffi:: lua_resetthread ( thread_state) ;
0 commit comments