GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-59705: Implement _thread.set_name() on Windows by vstinner · Pull Request #128675 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Jan 9, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Implement set_name() with SetThreadDescription() and _get_name() with GetThreadDescription(). If SetThreadDescription() or GetThreadDescription() is not available in kernelbase.dll, delete the method when the _thread module is imported.

Truncate the thread name to 32766 characters.

set_name() raises ValueError if the name contains an embedded null character.

  • Issue: Python should support exporting thread names to the OS #59705

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner @eryksun
pythongh-59705: Implement _thread.set_name() on Windows
479c7fb
Implement set_name() with SetThreadDescription() and _get_name() with
GetThreadDescription(). If SetThreadDescription() or
GetThreadDescription() is not available in kernelbase.dll, delete the
method when the _thread module is imported.

Truncate the thread name to an arbitrary limit of 64 characters.

set_name() raises ValueError if the name contains an embedded null
character.

Co-authored-by: Eryk Sun <[email protected]>
@vstinner vstinner added the skip news label Jan 9, 2025
@vstinner
vstinner requested a review from a team as a code owner January 9, 2025 12:35
@bedevere-app bedevere-app Bot mentioned this pull request Jan 9, 2025
Python should support exporting thread names to the OS #59705
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jan 9, 2025
@vstinner

vstinner commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

Code based on @eryksun's code: #59705 (comment). Differences with his code:

  • I only look for functions in kernelbase.dll, not in api-ms-win-core-processthreads-l1-1-3.dll (kernel32).
  • set_name() doesn't ignore E_NOTIMPL. That's fine since the threading module ignores any OSError. If Python is run on Windows older than 10.0.14393 (1607), set_name() failure would just be ignored silently.
  • I used the PyUnicode API to convert to/from wchar_t*.

Truncate the thread name to an arbitrary limit of 64 characters.

I copied this limit from @eryksun's code. Maybe it can be extended to 32766 characters?

set_name() raises ValueError if the name contains an embedded null character.

If it's a blocker issue, I can write the code differently to truncate to the first null character instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Embedded null characters are accepted
d588103
@vstinner

vstinner commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

set_name() raises ValueError if the name contains an embedded null character.

I misunderstood the code, in fact, the name is truncated at the first null character (ValueError is not raised): same behavior than Linux/macOS/FreeBSD/etc.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jan 13, 2025

Copy link
Copy Markdown
Member Author

cc @zooba @encukou

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jan 13, 2025

Copy link
Copy Markdown
Member Author

The alternative to SetThreadDescription() (Windows 10 and newer) is to call RaiseException(0x406D1388, ...). Example: msys2-contrib/mingw-w64@0d95c79. From what I understsood, RaiseException(...) is only understood by debuggers.

The Process Explorer tool doesn't show thread names.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Jan 14, 2025

Copy link
Copy Markdown
Member

I'm not familiar with Windows API, but this looks right to me. A review from @python/windows-team would be great though :)

(Specifically: this is a best-effort attempt to add info to aid debugging or understanding what's going on in a system: we want to as much as we reasonably can to the OS, but it's OK drop part of the name or not set it at all, and errors should be discarded since the user didn't really ask for this. And _get_name is private (test-only); this is not a reliable API to get back a Python string.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Fix trailing surrogate characters
8f2ead7
@vstinner

vstinner commented Jan 16, 2025

Copy link
Copy Markdown
Member Author

I plan to merge this change at the beginning of next week (January 20th).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jan 16, 2025

Copy link
Copy Markdown
Member Author

I fixed the truncation for surrogate pairs and I added tests with non-BMP characters (creating surrogate pairs on Windows).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Jan 17, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

I am not a Windows expert, but the code LGTM. Although I am not sure that we should set such small artificial limit.

There are also some issues in tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_threading.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_threading.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Add more non-BMP tests; fix test on Windows
f1c5232
serhiy-storchaka
serhiy-storchaka approved these changes Jan 17, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jan 17, 2025
vstinner added 2 commits January 17, 2025 13:17
@vstinner
Set limit to 32766
ba09ba1
@vstinner
Fix set_name() error handling
8148a96
@vstinner

vstinner commented Jan 17, 2025

Copy link
Copy Markdown
Member Author

Although I am not sure that we should set such small artificial limit.

Ok, I increased the limit from 64 to 32766 characters.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner enabled auto-merge (squash) January 17, 2025 12:20
@vstinner

vstinner commented Jan 17, 2025

Copy link
Copy Markdown
Member Author

Thanks for the reviews. I enabled auto-merge.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zooba
zooba reviewed Jan 17, 2025
View reviewed changes
Comment thread Modules/_threadmodule.c

#ifdef MS_WINDOWS
HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll");
if (kernelbase != NULL) {

@zooba zooba Jan 17, 2025

Copy link
Copy Markdown
Member

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.

If this check fails, we likely want to report a system error since everything is broken, but at least we should call DelAttr to remove the functions, since the function pointers will be null.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Jan 17, 2025

Copy link
Copy Markdown
Member Author

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.

set_name()/_get_name() is a minor feature of the _thread module, I would prefer to not prevent to import _thread if loading kernelbase.dll fails for whatever reason.

but at least we should call DelAttr to remove the functions, since the function pointers will be null.

Oops, my code was wrong. Fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread PC/pyconfig.h.in Outdated

// Truncate the thread name to 64 characters. The OS limit is 32766 wide
// characters, but long names aren't of practical use.
#define PYTHREAD_NAME_MAXLEN 64

@zooba zooba Jan 17, 2025

Copy link
Copy Markdown
Member

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.

There's no reason for us to artificially limit this at all, but I'd rather be much closer to the real limit than this. "Aren't of practical use" is a value judgement, not a technical limit.

Also, there's no C API for this, so we probably don't need a public C constant for the limit.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Jan 17, 2025

Copy link
Copy Markdown
Member Author

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.

I changed the limit to 32766 characters.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zooba reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@vstinner vstinner Jan 17, 2025

Copy link
Copy Markdown
Member Author

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.

Also, there's no C API for this, so we probably don't need a public C constant for the limit.

I can rename the macro use _Py prefix. But that unrelated to the Windows implementation, so I would prefer to do it in a separated change. Non-Windows platforms use the same macro.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Jan 17, 2025

Copy link
Copy Markdown
Member Author

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.

Also, there's no C API for this, so we probably don't need a public C constant for the limit.

I created #128945 to make the macro private.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@zooba
zooba disabled auto-merge January 17, 2025 12:23
@vstinner
Fix thread_module_exec()
0ff0567
Remove the module methods if GetModuleHandleW() fails.
@vstinner

vstinner commented Jan 17, 2025

Copy link
Copy Markdown
Member Author

@zooba: Please review the updated PR. I fixed thread_module_exec() to remove the module methods if GetModuleHandleW() fails.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zooba

zooba commented Jan 17, 2025

Copy link
Copy Markdown
Member

LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit d7f703d into python:main Jan 17, 2025
@vstinner
vstinner deleted the win_thread_descr branch January 17, 2025 13:55
@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 17, 2025
@vstinner

vstinner commented Jan 17, 2025

Copy link
Copy Markdown
Member Author

Merged. Thanks for reviews.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 21, 2025
@vstinner @eryksun @srinivasreddy
pythongh-59705: Implement _thread.set_name() on Windows (python#128675)
5823ad8
Implement set_name() with SetThreadDescription() and _get_name() with
GetThreadDescription(). If SetThreadDescription() or
GetThreadDescription() is not available in kernelbase.dll, delete the
method when the _thread module is imported.

Truncate the thread name to 32766 characters.

Co-authored-by: Eryk Sun <[email protected]>
@python python deleted a comment Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba zooba left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@vstinner @encukou @zooba @serhiy-storchaka
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.