Skip to content

feat(go_port): reproduce race condition with object and class - #888

Open
Schrodinger257 wants to merge 1 commit into
metacall:developfrom
Schrodinger257:go_port_reproduce_object_race_condition
Open

Schrodinger257 wants to merge 1 commit into
metacall:developfrom
Schrodinger257:go_port_reproduce_object_race_condition

Conversation

@Schrodinger257

Copy link
Copy Markdown

Description

In object (class) when calling get or set or call, GO passes o.ptr (c.ptr) to the c function. if the c function was doing a work that takes long time to execute before storing the pointer passed to it from GO and GO garbage collector ran while c function still working it will see the object (class) as unreachable and as a result it will call their finalizers and free them which causes a segfault when the c function tries to access it.

In this PR i reproduced the race condition as follows:
first creating a py script in memory and load py function that return a class. after it i ran a goroutine that calls garbage collector in the background many times and created an object from the class and called Get("width") on it which triggered the problem and caused a segfault

signal: segmentation fault (core dumped)
FAIL    github.com/metacall/core/source/ports/go_port/source    1.771s

using defer runtime.KeepAlive() tells the garbage collector that the object (class) must be kept alive during the execution of Get() function and never try to free or call its finalizer. and this solved the problem

width = 10
PASS
ok      github.com/metacall/core/source/ports/go_port/source    1.208s

Fixes in PR #875

in object and class when calling get or set or call, GO passes o.ptr or c.ptr to the c function. if the c function was doing a work that takes long time to execute before storing the pointer passed to it from GO and GO garbage collector ran while c function still working it will see the object or calss as unreachable as a result it will call their finalizers and free them which causes a segfault when the c function tries to access it
@Schrodinger257 Schrodinger257 mentioned this pull request Sep 16, 2026
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant