Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63b20f7

Browse files
committed
coroutine, bugfix: do not recycle fiber objects during GC.
1 parent b614656 commit 63b20f7

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

fibjs/include/Fiber.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class JSFiber : public Fiber_base,
3232

3333
public:
3434
obj_ptr<JSFiber> m_pFiber;
35+
v8::Global<v8::Object> m_fiber;
3536
TryCatch try_catch;
3637
};
3738

fibjs/src/base/Runtime.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,15 @@ void Isolate::init()
316316
v8::Isolate::Scope isolate_scope(m_isolate);
317317
v8::HandleScope handle_scope(m_isolate);
318318

319-
JSFiber::EnterJsScope s;
320-
321319
v8::Local<v8::Context> _context = v8::Context::New(m_isolate);
322320
m_context.Reset(m_isolate, _context);
323321

324322
v8::Context::Scope context_scope(_context);
325323

324+
Fiber_base::class_info().getModule(this);
325+
326+
JSFiber::EnterJsScope s;
327+
326328
if (g_cov && m_id == 1)
327329
beginCoverage(m_isolate);
328330

fibjs/src/coroutine/Fiber.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ JSFiber::EnterJsScope::EnterJsScope(JSFiber* fb)
221221

222222
exlib::Fiber::tlsPut(g_tlsCurrent, m_pFiber);
223223
m_pFiber->holder()->m_fibers.putTail(m_pFiber);
224+
225+
m_fiber.Reset(m_pFiber->holder()->m_isolate, m_pFiber->wrap());
224226
}
225227

226228
JSFiber::EnterJsScope::~EnterJsScope()

test/coroutine_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe('coroutine', () => {
9292
assert.equal(n, 1300);
9393
});
9494

95+
it('do not recycle fiber objects during GC', () => {
96+
coroutine.current().v = 100;
97+
GC();
98+
assert.equal(coroutine.current().v, 100);
99+
});
100+
95101
it('caller', () => {
96102
function t_fiber3(v1, v2) {
97103
n = v1 + v2 + this.caller.v;

0 commit comments

Comments
 (0)