-
Notifications
You must be signed in to change notification settings - Fork 277
Remove incorrect Py_INCREF/DECREF on Model in catchEvent/dropEvent #1190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Joao-Dionisio
wants to merge
6
commits into
master
Choose a base branch
from
fix-catchEvent-refcount
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4507e2
Remove spurious Py_INCREF/DECREF on Model in catchEvent/dropEvent
Joao-Dionisio 6f80a47
Merge branch 'master' into fix-catchEvent-refcount
Joao-Dionisio 8e76c13
Apply suggestions from code review
Joao-Dionisio c36cb0d
Update tests/test_event.py
Joao-Dionisio fda89d8
Update tests/test_event.py
Joao-Dionisio 12d901a
Add changelog entries for unreleased changes
Joao-Dionisio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |||||
|
|
||||||
| ## Unreleased | ||||||
| ### Added | ||||||
| - Added `getMemUsed()`, `getMemTotal()`, and `getMemExternEstim()` methods | ||||||
| ### Fixed | ||||||
| - Removed incorrect `Py_INCREF`/`Py_DECREF` on `Model` in `catchEvent`/`dropEvent` that caused reference count imbalance | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ### Changed | ||||||
| ### Removed | ||||||
|
|
||||||
|
|
||||||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import pytest, random | ||
| import pytest, weakref, gc, random | ||
|
|
||
| from pyscipopt import Model, Eventhdlr, SCIP_RESULT, SCIP_EVENTTYPE, SCIP_PARAMSETTING, quicksum | ||
|
|
||
|
|
@@ -189,4 +189,40 @@ def eventexec(self, event): | |
| m.includeEventhdlr(ev, "var_event", "event handler for var events") | ||
|
|
||
| with pytest.raises(Exception): | ||
| m.optimize() | ||
| m.optimize() | ||
|
|
||
| def test_catchEvent_does_not_leak_model(): | ||
| """catchEvent should not artificially increment the Model's reference count. | ||
|
|
||
| Previously, catchEvent called Py_INCREF(self) on the Model, and dropEvent | ||
| called Py_DECREF(self). Since many event handlers skip dropEvent (e.g. when | ||
| self.model is already dead), this caused the Model to leak — its refcount | ||
| never returned to zero, preventing garbage collection. | ||
|
Comment on lines
+199
to
+200
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence feels quite incomplete, but well. |
||
| """ | ||
|
|
||
| class SimpleEvent(Eventhdlr): | ||
| def eventinit(self): | ||
| self.model.catchEvent(SCIP_EVENTTYPE.NODEFOCUSED, self) | ||
|
|
||
| def eventexit(self): | ||
| pass # intentionally no dropEvent, which is bad practice | ||
|
|
||
| def eventexec(self, event): | ||
| pass | ||
|
|
||
| m = Model() | ||
| m.hideOutput() | ||
| ev = SimpleEvent() | ||
| m.includeEventhdlr(ev, "simple", "test event handler") | ||
| m.addVar("x", obj=1, vtype="I") | ||
| m.optimize() | ||
|
|
||
| ref = weakref.ref(m) | ||
|
|
||
| del ev | ||
| gc.collect() | ||
| assert ref() is not None, "Model was garbage collected — event handler absorbed a reference" | ||
|
|
||
Joao-Dionisio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| del m | ||
| gc.collect() | ||
| assert ref() is None, "Model was not garbage collected — catchEvent likely leaked a reference" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably that we do not have to worry about memory anymore because we know how to measure it.