Created
September 16, 2011 07:00
-
-
Save lubyk/1221403 to your computer and use it in GitHub Desktop.
Lua garbage collection weirdness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~~~ = link from table with weak values | |
******************************************************************************** | |
State before removing 'foobar': | |
+----------------------------------------+ | |
| | | |
v | | |
scheduler ---> wrap ---> t ~~~> thread ---> coroutine --- (up-value) --> foobar ---> udata | |
^ ^ | |
| | | |
main ----------------------------------------------------------------------+ | |
******************************************************************************** | |
[EDIT] Dependency graph in foobar (makes the bug appear) | |
+-------- stack -------------------+ | |
| | | |
v | | |
foobar ---> udata ---> fenv ---> udata_thread | |
^ | | |
| | | |
+---------+ | |
The bug only appears when 'foobar' is in udata_thread's stack. | |
******************************************************************************** | |
******************************************************************************** | |
Setting 'foobar' to nil | |
main.foobar = nil | |
collectgarbage() | |
+----------------------------------------+ | |
| | | |
v | | |
scheduler ---> wrap ---> t ~~~> thread ---> coroutine --- (up-value) --> foobar ---> 0x0 udata | |
^ | |
| | |
main | |
The coroutine exists and when the scheduler calls | |
coroutine.resume(wrap.t.thread.co) we SEGFAULT. | |
******************************************************************************** | |
******************************************************************************** | |
main.foobar.thread = nil | |
main.foobar = nil | |
collectgarbage() | |
scheduler ---> wrap ---> t | |
^ | |
| | |
main | |
Coroutine removed with 'foobar': all OK | |
******************************************************************************** | |
******************************************************************************** | |
Complete dependency graph | |
main.foobar = nil | |
collectgarbage() | |
+----------------------------------------+ | |
| | | |
v | | |
scheduler ---> wrap ---> t ~~~> thread ---> coroutine --- (up-value) --> foobar ---> 0x0 udata | |
^ | |
| | |
main | |
The coroutine exists and when the scheduler calls | |
coroutine.resume(wrap.t.thread.co) we SEGFAULT. | |
******************************************************************************** | |
******************************************************************************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment