diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-07-04 09:12:32 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-07-04 09:12:32 +0200 |
| commit | f37a7c2334fdfb8f30d0b76ecf25aff484adec28 (patch) | |
| tree | 2385dc68660e8cc15dd9cc7172e2d0f53740dd01 | |
| parent | acb1fa879beb68c10287b8438f18b80b2700042f (diff) | |
| download | lanes-f37a7c2334fdfb8f30d0b76ecf25aff484adec28.tar.gz lanes-f37a7c2334fdfb8f30d0b76ecf25aff484adec28.tar.bz2 lanes-f37a7c2334fdfb8f30d0b76ecf25aff484adec28.zip | |
Fix finalizers in coroutine lanes
| -rw-r--r-- | src/lane.cpp | 6 | ||||
| -rw-r--r-- | tests/cancel.lua | 11 |
2 files changed, 11 insertions, 6 deletions
diff --git a/src/lane.cpp b/src/lane.cpp index 10060ad..e045d88 100644 --- a/src/lane.cpp +++ b/src/lane.cpp | |||
| @@ -813,8 +813,10 @@ static void lane_main(Lane* const lane_) | |||
| 813 | push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] | 813 | push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] |
| 814 | 814 | ||
| 815 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); | 815 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); |
| 816 | // Call finalizers, if the script has set them up. | 816 | // Call finalizers, if the script has set them up. |
| 817 | LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; | 817 | // If the lane is not a coroutine, there is only a regular state, so everything is the same whether we use S or L. |
| 818 | // If the lane is a coroutine, this has to be done from the master state (S), not the thread (L), because we can't lua_pcall in a thread state | ||
| 819 | LuaError const _rc2{ run_finalizers(lane_->S, lane_->errorTraceLevel, _rc) }; | ||
| 818 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl); | 820 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl); |
| 819 | if (_rc2 != LuaError::OK) { // Error within a finalizer! | 821 | if (_rc2 != LuaError::OK) { // Error within a finalizer! |
| 820 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack | 822 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack |
diff --git a/tests/cancel.lua b/tests/cancel.lua index 9ee986c..8dfccd2 100644 --- a/tests/cancel.lua +++ b/tests/cancel.lua | |||
| @@ -26,6 +26,9 @@ local linda = lanes.linda() | |||
| 26 | -- a numeric value to read | 26 | -- a numeric value to read |
| 27 | linda:set( "val", 33.0) | 27 | linda:set( "val", 33.0) |
| 28 | 28 | ||
| 29 | -- so that we can easily swap between lanes.gen and lanes.coro, to try stuff | ||
| 30 | local generator = lanes.coro | ||
| 31 | |||
| 29 | -- ################################################################################################## | 32 | -- ################################################################################################## |
| 30 | 33 | ||
| 31 | if not next(which_tests) or which_tests.genlock then | 34 | if not next(which_tests) or which_tests.genlock then |
| @@ -149,7 +152,7 @@ end | |||
| 149 | if not next(which_tests) or which_tests.linda then | 152 | if not next(which_tests) or which_tests.linda then |
| 150 | remaining_tests.linda = nil | 153 | remaining_tests.linda = nil |
| 151 | print "\n\n####################################################################\nbegin linda cancel test\n" | 154 | print "\n\n####################################################################\nbegin linda cancel test\n" |
| 152 | h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda | 155 | h = generator( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda |
| 153 | 156 | ||
| 154 | print "wait 1s" | 157 | print "wait 1s" |
| 155 | SLEEP(1) | 158 | SLEEP(1) |
| @@ -170,7 +173,7 @@ end | |||
| 170 | if not next(which_tests) or which_tests.soft then | 173 | if not next(which_tests) or which_tests.soft then |
| 171 | remaining_tests.soft = nil | 174 | remaining_tests.soft = nil |
| 172 | print "\n\n####################################################################\nbegin soft cancel test\n" | 175 | print "\n\n####################################################################\nbegin soft cancel test\n" |
| 173 | h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda | 176 | h = generator( "*", protectedBody)( "receive") -- start an infinite wait on the linda |
| 174 | 177 | ||
| 175 | print "wait 1s" | 178 | print "wait 1s" |
| 176 | SLEEP(1) | 179 | SLEEP(1) |
| @@ -194,7 +197,7 @@ end | |||
| 194 | if not next(which_tests) or which_tests.hook then | 197 | if not next(which_tests) or which_tests.hook then |
| 195 | remaining_tests.hook = nil | 198 | remaining_tests.hook = nil |
| 196 | print "\n\n####################################################################\nbegin hook cancel test\n" | 199 | print "\n\n####################################################################\nbegin hook cancel test\n" |
| 197 | h = lanes.gen( "*", protectedBody)( "get", 300000) | 200 | h = generator( "*", protectedBody)( "get", 300000) |
| 198 | print "wait 2s" | 201 | print "wait 2s" |
| 199 | SLEEP(2) | 202 | SLEEP(2) |
| 200 | 203 | ||
| @@ -230,7 +233,7 @@ end | |||
| 230 | if not next(which_tests) or which_tests.hard_unprotected then | 233 | if not next(which_tests) or which_tests.hard_unprotected then |
| 231 | remaining_tests.hard_unprotected = nil | 234 | remaining_tests.hard_unprotected = nil |
| 232 | print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" | 235 | print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" |
| 233 | h = lanes.gen( "*", laneBody)( "receive", nil) | 236 | h = generator( "*", laneBody)( "receive", nil) |
| 234 | 237 | ||
| 235 | -- wait 2s before cancelling the lane | 238 | -- wait 2s before cancelling the lane |
| 236 | print "wait 2s" | 239 | print "wait 2s" |
