FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-127604: Don't rely on `dprintf()` for faulthandler C stacks by ZeroIntensity · Pull Request #132800 · python/cpython · GitHub

/ cpython Public

gh-127604: Don't rely on dprintf() for faulthandler C stacks - #132800

Closed
ZeroIntensity wants to merge 5 commits into
python:mainfrom
ZeroIntensity:faulthandler-c-stack-fix
Closed

gh-127604: Don't rely on dprintf() for faulthandler C stacks#132800
ZeroIntensity wants to merge 5 commits into
python:mainfrom
ZeroIntensity:faulthandler-c-stack-fix

Conversation

ZeroIntensity commented Apr 22, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

I thought it wasn't possible for a system to have glibc's backtrace() but not glibc's dprintf(). Apparently, I was wrong.
This uses fdopen()/vfprintf() instead, which should be portable across anything POSIX, and even Windows.

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @ZeroIntensity for commit dc39d9a 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F132800%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 22, 2025

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Did you forget fclose(). I think that you need also dup().

Copy link
Copy Markdown
Member Author

Should be fixed now.

Comment thread Python/fileutils.c Outdated
int res = vfprintf(handle, fmt, vargs);
va_end(vargs);
fclose(handle);
close(newfd);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You don't need close() here, because fclose() closes the file descriptor.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry, I'm not used to going between FILE * and int like this. Fixed it.

Copy link
Copy Markdown
Member Author

I'm not sure what to do about WASM lacking dup.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

close() is still used when fopen() fails.

On WASM, an alternative implementation can use vsnpritf() and a fixed-size buffer. Does WASM even support access to a call stack?

Also, some platforms can use dprintf() directly, instead of a fallback.

Copy link
Copy Markdown
Member Author

close() is still used when fopen() fails.

That's intentional. fdopen() doesn't close the file descriptor on failure, does it?

I'd like to lean away from dprintf, because apparently, it comes with its own set of problems. There are some POSIX implementations that don't implement it correctly, and I don't think it's worth adding something like a _POSIX_SOURCE guard here

Copy link
Copy Markdown
Member

I meant that removing the define was premature if the define was needed. But there is no compiling error, so it may be unneeded.

Copy link
Copy Markdown
Member Author

It's a deprecated alias on Windows, but I don't think they'll go removing it anytime soon. I'll add it back for completeness.

Copy link
Copy Markdown
Member Author

It looks like there is a _Py_dup function that we can use for WASI, but it's not heap-safe when exceptions occur. Signal handlers in faulthandler can't use the heap, so any of the PyErr calls are unsafe.

I guess we could add an internal-only _PyErr_SetFromErrnoHeapsafe that only raises when we're not in a signal handler.

Copy link
Copy Markdown
Member

@vstinner, do we need to use _Py_dup() instead of dup() in this case?

Copy link
Copy Markdown
Member

_Py_dup() is more complicated to ensure that the file descriptor is non-inheritable. I don't think that it's useful here.

Comment thread Python/traceback.c
|| info[i].dli_fname[0] == '\0'
) {
dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);
_Py_fdprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It would be safer to not use printf family but use traceback.c functions to write directly into the fd.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, but then we have to deal with stack limits. I'll probably end up using those regardless, though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I wrote #132854 to replace dprintf() with _Py_write_noraise().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, but then we have to deal with stack limits

Which stack limits? traceback.c code is designed to use little stack memory and be async-signal safe.

ZeroIntensity Apr 23, 2025
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I was thinking of a sprintf that would be incrementally printed. Your solution works a lot better.

Copy link
Copy Markdown
Member Author

Closing in favor of #132854

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL