Skip to content

Instantly share code, notes, and snippets.

@ochaton
Last active November 3, 2020 21:03
Show Gist options
  • Select an option

  • Save ochaton/79ca7ae7e91d4b23e156be6cb327ae2e to your computer and use it in GitHub Desktop.

Select an option

Save ochaton/79ca7ae7e91d4b23e156be6cb327ae2e to your computer and use it in GitHub Desktop.
Fibers collected by gc even if they still alive
#!/usr/bin/env tarantool
print(_G._TARANTOOL)
-- box.cfg{}
require 'jit.dump'.start("hotloop=1")
local fiber = require 'fiber'
local log = require 'log'
local json = require 'json'
json.cfg{ encode_use_tostring = true }
local function gc()
collectgarbage('collect')
collectgarbage('collect')
end
local registry = setmetatable({}, { __mode = 'v' })
local id = 0
local evaluate
local setgc = function(f)
f = f or fiber.self()
debug.getmetatable(f).__gc = function(me)
print("__gc called for ", pcall(tostring, me))
end
end
local function runit(sleep)
id = id + 1
local udata = fiber.create(evaluate, sleep)
registry[tostring(id)] = udata
gc()
log.info(json.encode(registry))
end
function evaluate(sleep)
local me = fiber.self()
setgc()
log.info("Started", fiber.id())
fiber.sleep(sleep)
log.info("Finished %s", fiber.id())
return
end
for _ = 1, 5 do
runit(1+math.random()+math.random(0,1))
end
fiber.create(function()
fiber.name("watcher")
while true do
log.info("watcher", json.encode(registry))
fiber.sleep(0.5)
gc()
end
end)
@ochaton
Copy link
Copy Markdown
Author

ochaton commented Nov 3, 2020

2.6.1-11-g37077a48e
Started
{"1":{"status":"suspended","name":"lua","id":104}}
Started
{"1":{"status":"suspended","name":"lua","id":104},"2":{"status":"suspended","name":"lua","id":105}}
Started
{"2":{"status":"suspended","name":"lua","id":105},"1":{"status":"suspended","name":"lua","id":104},"3":{"status":"suspended","name":"lua","id":106}}
Started
{"2":{"status":"suspended","name":"lua","id":105},"4":{"status":"suspended","name":"lua","id":107},"1":{"status":"suspended","name":"lua","id":104},"3":{"status":"suspended","name":"lua","id":106}}
Started
{"4":{"status":"suspended","name":"lua","id":107},"1":{"status":"suspended","name":"lua","id":104},"5":{"status":"suspended","name":"lua","id":108},"2":{"status":"suspended","name":"lua","id":105},"3":{"status":"suspended","name":"lua","id":106}}
watcher
entering the event loop
watcher
watcher
watcher
watcher
Finished 106
Finished 107
Finished 108
watcher
Finished 105
Finished 104
watcher

I was expect to receive dump of registry inside watcher fiber when fibers were still alive

@ochaton
Copy link
Copy Markdown
Author

ochaton commented Nov 3, 2020

__gc is executed for each fiber after they really finished

1.10.6-53-gc1ae2bb88
Started
{"1":{"status":"suspended","name":"lua","id":102}}
Started
{"1":{"status":"suspended","name":"lua","id":102},"2":{"status":"suspended","name":"lua","id":103}}
Started
{"2":{"status":"suspended","name":"lua","id":103},"1":{"status":"suspended","name":"lua","id":102},"3":{"status":"suspended","name":"lua","id":104}}
Started
{"2":{"status":"suspended","name":"lua","id":103},"4":{"status":"suspended","name":"lua","id":105},"1":{"status":"suspended","name":"lua","id":102},"3":{"status":"suspended","name":"lua","id":104}}
Started
{"4":{"status":"suspended","name":"lua","id":105},"1":{"status":"suspended","name":"lua","id":102},"5":{"status":"suspended","name":"lua","id":106},"2":{"status":"suspended","name":"lua","id":103},"3":{"status":"suspended","name":"lua","id":104}}
watcher
entering the event loop
__gc called for 	true	fiber: 107
watcher
watcher
watcher
watcher
Finished 104
Finished 105
Finished 106
__gc called for 	false	the fiber is dead
__gc called for 	false	the fiber is dead
__gc called for 	false	the fiber is dead
watcher
Finished 103
Finished 102
__gc called for 	false	the fiber is dead
__gc called for 	false	the fiber is dead
watcher
watcher
watcher
watcher
watcher
watcher
watcher
watcher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment