Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-123504: Fix reference leak in initialization of_tkinter#123505

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 9 commits intopython:mainfromZeroIntensity:tkinter-refleak
Sep 3, 2024

Conversation

@ZeroIntensity
Copy link
Member

@ZeroIntensityZeroIntensity commentedAug 30, 2024
edited by bedevere-appbot
Loading

@picnixz
Copy link
Member

What is the output./python -Xshowrefcount -c "import tkinter" with this PR?

@ZeroIntensity
Copy link
MemberAuthor

What is the output./python -Xshowrefcount -c "import tkinter" with this PR?

[0 refs, 0 blocks]. Though, running with Valgrind is showing some (non-reference) leaks fromTcl_FindExecutable -- that's a separate issue and out of the scope of this PR, though.

picnixz reacted with thumbs up emoji


Tkinter_TclError=PyErr_NewException("_tkinter.TclError",NULL,NULL);
if (PyModule_AddObjectRef(m,"TclError",Tkinter_TclError)) {
if (PyModule_AddObject(m,"TclError",Tkinter_TclError)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Tkinter_TclError should be a strong reference to_tkinter.TclError. Even if you remove "TclError" from the the module dict, the use ofTkinter_TclError should not crash.

BTW, do not usePyModule_AddObject(), it is broken beyond repair. UsePyModule_AddObjectRef() orPyModule_Add(), what is more convenient.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ok, where should the strong reference toTkinter_TclError be decref'd? Should we add a module clear function?

Py_DECREF(m);
returnNULL;
}
Py_DECREF(Tkapp_Type);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It is the same for all other global references (otherwise why would they be global references?).

Yes, if you want to release these references, you need a module clearing function. This is not a big deal, because usually when you unload the module, you quit the program, so who cares about few dangling references?

ZeroIntensity reacted with thumbs up emoji
@ZeroIntensity
Copy link
MemberAuthor

FWIW, it might be worth trying to switch_tkinter over toPEP-489 style initialization in the future.

@picnixz
Copy link
Member

FWIW, it might be worth trying to switch _tkinter

I had those same concerns when dealing with curses in#123291 but since the module is a bit of a mess, I just focused on getting the correct DECREF when needed.

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM, it works as expected.

Without this PR:

$ ./python -Xshowrefcount -c "import tkinter"[130 refs, 78 blocks]

With this PR:

$ ./python -Xshowrefcount -c "import tkinter"[0 refs, 0 blocks]

…J9_BB.rstCo-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM.

PyModuleDef_HEAD_INIT,
"_tkinter",
NULL,
-1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You removed.m_size = -1, but it's ok,m_size=0 also means "no module state".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There is a difference between 0 and -1 related to subinterpreters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What is the difference?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

From the documentation:

      Setting ``m_size`` to ``-1`` means that the module does not support      sub-interpreters, because it has global state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Oh I see, I forgot about this difference :-(

@vstinnervstinnerenabled auto-merge (squash)September 3, 2024 20:26
@vstinner
Copy link
Member

I enabled auto-merge. Thanks for the PR updates.

I don't think that such "leak" at Python exit is important enough to justify a backport.

ZeroIntensity reacted with thumbs up emoji

@vstinnervstinner merged commita8bc036 intopython:mainSep 3, 2024
@ZeroIntensityZeroIntensity deleted the tkinter-refleak branchSeptember 3, 2024 23:09
PyModuleDef_HEAD_INIT,
"_tkinter",
NULL,
-1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There is a difference between 0 and -1 related to subinterpreters.

NULL
.m_name="_tkinter",
.m_methods=moduleMethods,
.m_clear=module_clear,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If you define "clear", you should always define "travel".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would it make sense to emit a warning or even raise an error at runtime?

For example in Objects/typeobject.c, I added an assertion in _PyType_CheckConsistency():

if (type->tp_flags&Py_TPFLAGS_HAVE_GC) {// bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.// Note: tp_clear is optional.CHECK(type->tp_traverse!=NULL);    }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks like a good idea. For both types and modules.

vstinner reacted with thumbs up emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@vstinnervstinnervstinner approved these changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@picnixzpicnixzpicnixz left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@ZeroIntensity@picnixz@vstinner@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp