Last active
November 3, 2020 21:03
-
-
Save ochaton/79ca7ae7e91d4b23e156be6cb327ae2e to your computer and use it in GitHub Desktop.
Fibers collected by gc even if they still alive
This file contains hidden or 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
| #!/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) |
Author
Author
__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
I was expect to receive dump of
registryinside watcher fiber when fibers were still alive