Decrement reference count of old token when template token is replaced.#581
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to fix issue #580 by preventing a ref-count–related memory leak when a template token is replaced, ensuring the old template token’s reference is released when a new token is created.
Changes:
- Update
_lf_get_token()to release the template’s reference to the old shared token when allocating a new token. - Install the newly created token as
tmplt->tokenrather than leaving the template pointing at the old shared token.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This fix exposed another bug which caused the Python test ConcurrentActionRepeat.lf to fail intermittently. Claud's analysis: The remaining raceEven with the env lock held across
This race was always present in principle but was masked before by an extra "phantom" ref (the original memory leak). The leak fix removed the phantom, making the race reliably reproducible. The fix
LF_CRITICAL_SECTION_ENTER(env);
// Check to see if value exists
if (value) {
// Allocate a fresh token for this schedule call rather than routing through
// _lf_initialize_token_with_value / _lf_get_token. Those paths may reuse
// or replace trigger->tmplt.token, which races with the reaction prologue
// that reads trigger->tmplt.token->value after an event pop:
// ...
trigger->tmplt.type.element_size = sizeof(PyObject*);
t = lf_new_token((void*)&trigger->tmplt, value, 1);
#if !defined NDEBUG
LF_CRITICAL_SECTION_ENTER(GLOBAL_ENVIRONMENT);
extern int _lf_count_payload_allocations;
_lf_count_payload_allocations++;
LF_CRITICAL_SECTION_EXIT(GLOBAL_ENVIRONMENT);
#endif
Py_INCREF(value);
act->value = value;
}
lf_schedule_trigger(env, trigger, offset, t);
lf_notify_of_event(env);
LF_CRITICAL_SECTION_EXIT(env);Now:
This mirrors the same pattern that |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This PR addresses #580.