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-134604: Fix tracemalloc crash with subinterpreters#134667

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

Conversation

emmatyping
Copy link
Member

@emmatypingemmatyping commentedMay 25, 2025
edited by bedevere-appbot
Loading

This is an attempt at resolving a crash when mixing tracemalloc and sub-interpreters I came across recently. My investigation is recorded in the issue#134604 (comment)

This PR makes a few changes:

  • modifies tracemalloc to copy the filename of a frame instead of referencing it
  • delays sub-interpreter finalization until after tracemalloc finalizes (optional, but probably a good idea)
  • fixes a typo/duplicated assignment on a line in the tracemalloc_get_frame function
  • adds tests totest__interpreters to ensure that this functions correctly.

Should this be backported to 3.13? I suppose subinterpreters can be used there through C APIs, so it might be good to fix this there too.

With subinterpreters, frames can be destroyed at any point, meaningtracemalloc needs to copy instead of reference the frame filenames.
Copy link
Member

@ZeroIntensityZeroIntensity left a comment

Choose a reason for hiding this comment

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

I think there's more to be fixed than just the filename. Doesn't tracemalloc have mutable state accessible by all interpreters?

@emmatyping
Copy link
MemberAuthor

I think there's more to be fixed than just the filename. Doesn't tracemalloc have mutable state accessible by all interpreters?

If you mean the allocator/tracemalloc runtime state, that is guarded by a lock.

@ZeroIntensity
Copy link
Member

Right, but some of the state that it stores looks like it could be problematic across interpreters. I'll investigate.

emmatyping reacted with thumbs up emoji

@emmatyping
Copy link
MemberAuthor

Yeah the following test hangs:

deftest_tracemalloc_multiple_subinterpreters(self):num_threads=8threads= []defrun_interp():interpid=_interpreters.create()_interpreters.run_string(interpid,"def foo(a, b): return a + b; foo(1, 5)")_interpreters.destroy(interpid)foriinrange(num_threads):thread=threading.Thread(target=run_interp)threads.append(thread)withthreading_helper.start_threads(threads):passstats=tracemalloc.take_snapshot().statistics("filename")count=sum(trace.traceback._frames[0][0]=="<string>"fortraceinstats)self.assertEqual(count,num_threads)
ZeroIntensity reacted with thumbs up emoji

@vstinner
Copy link
Member

modifies tracemalloc to copy the filename of a frame instead of referencing it

tracemalloc_filenames stores strong references to Unicode strings. What is the problem with sub-interpreters? I would prefer to keep Unicode objects. For example,_Py_DumpASCII(fd, filename) has a different behavior thanPUTS(fd, filename).

@vstinner
Copy link
Member

Would it be possible to modify memory allocation hooks (malloc, calloc, realloc, free) to detect which interpreter is used, and do nothing if it's called from an interpreter different than the one which called tracemalloc.start()? Limit tracemalloc to a single interpreter at the same time.

The problem is thattracemalloc_raw_malloc(),tracemalloc_raw_calloc(),tracemalloc_raw_realloc() andtracemalloc_free() are called without the GIL. The first 3 functions callPyGILState_Ensure() /PyGILState_Release() to acquire the GIL, but these functions are known to not work with sub-interpreters... We would needPEP 788 API here.

emmatyping reacted with thumbs up emoji

@emmatyping
Copy link
MemberAuthor

Would it be possible to modify memory allocation hooks (malloc, calloc, realloc, free) to detect which interpreter is used, and do nothing if it's called from an interpreter different than the one which called tracemalloc.start()? Limit tracemalloc to a single interpreter at the same time.

Yeah I think something like that could be done. I think it might be better to shard the state it tracks to be per-interpreter however. I expect users may wish to call tracemalloc.start() within a sub-interpreter. Also what would the behavior of-X tracemalloc be if it only traced one interpreter? Tracing the main one I presume?

@emmatyping
Copy link
MemberAuthor

I'm going to go ahead and close this PR since the solution here is not sufficient. I'll think more about a proper solution and maybe we can figure that out in the issue.

vstinner reacted with thumbs up emoji

@vstinner
Copy link
Member

Thanks for trying, it's a hard problem.

emmatyping reacted with heart emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@vstinnervstinnerAwaiting requested review from vstinner

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrently

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@emmatyping@ZeroIntensity@vstinner

[8]ページ先頭

©2009-2025 Movatter.jp