Skip to content
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

Signal handlers must preserve errno #54520

Closed
hfuru mannequin opened this issue Nov 4, 2010 · 12 comments
Closed

Signal handlers must preserve errno #54520

hfuru mannequin opened this issue Nov 4, 2010 · 12 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@hfuru
Copy link
Mannequin

hfuru mannequin commented Nov 4, 2010

BPO 10311
Nosy @loewis, @amauryfa, @pitrou
Files
  • signalmodule-errno.diff: patch: save/restore errno in signal handler
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-11-05.19:55:49.073>
    created_at = <Date 2010-11-04.12:18:17.342>
    labels = ['interpreter-core', 'type-bug']
    title = 'Signal handlers must preserve errno'
    updated_at = <Date 2010-11-05.19:55:49.071>
    user = 'https://bugs.python.org/hfuru'

    bugs.python.org fields:

    activity = <Date 2010-11-05.19:55:49.071>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-05.19:55:49.073>
    closer = 'pitrou'
    components = ['Interpreter Core']
    creation = <Date 2010-11-04.12:18:17.342>
    creator = 'hfuru'
    dependencies = []
    files = ['19488']
    hgrepos = []
    issue_num = 10311
    keywords = ['patch']
    message_count = 12.0
    messages = ['120395', '120397', '120400', '120403', '120405', '120406', '120409', '120412', '120413', '120479', '120481', '120528']
    nosy_count = 5.0
    nosy_names = ['loewis', 'exarkun', 'amaury.forgeotdarc', 'hfuru', 'pitrou']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10311'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 4, 2010

    Signal handlers that can change errno, must restore it.
    I enclose a patch for <2.7, 3.2a3>/Modules/signalmodule.c
    which also rearranges the code to make this a bit easier.

    The patch does if (errno != save_errno) errno = save_errno;
    instead of just errno = save_errno;
    in case it's less thread-safe to write than to read errno,
    which would not surprise me but may be pointless paranoia.

    I don't know what needs to be done on non-Unix systems,
    like Windows' WSAError stuff.

    @hfuru hfuru mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 4, 2010
    @pitrou
    Copy link
    Member

    pitrou commented Nov 4, 2010

    This is a good idea IMO. It would be better if you minimized style changes, so that the patch is easier to review.
    Also, is the while loop around write() necessary here?

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 4, 2010

    Parser/intrcheck.c:intcatcher() should do the same. Covered in Issue
    10312.

    Antoine Pitrou writes:

    This is a good idea IMO. It would be better if you minimized style
    changes, so that the patch is easier to review.

    I'm afraid the un-rearranged code would be fairly ugly, so I cleaned
    up first. Single exit point.

    Also, is the while loop around write() necessary here?

    Whoops, I'd forgotten I did that too, it was on my TODO list to
    check if Python makes it unnecessary by making write restartable.
    I don't remember if that's possible to ensure on all modern Posixes

    @amauryfa
    Copy link
    Member

    amauryfa commented Nov 4, 2010

    This issue is not really relevant on Windows:

    • signals are actually run in a new thread specially created.
    • errno is a thread-local variable; its value is thus local to the signal handler, same for WSAGetLastError().

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 4, 2010

    Amaury Forgeot d'Arc writes:

    This issue is not really relevant on Windows:

    • signals are actually run in a new thread specially created.
    • errno is a thread-local variable; its value is thus local to the
      signal handler, same for WSAGetLastError().

    Nice. Then I suggest a config macro for whether this is needed.
    Either a test for windows, or an autoconf thing in case some Unixes
    are equally sensible. (Linux isn't, I checked.)

    @amauryfa
    Copy link
    Member

    amauryfa commented Nov 4, 2010

    Nice. Then I suggest a config macro for whether this is needed.
    Either a test for windows, or an autoconf thing in case some Unixes
    are equally sensible. (Linux isn't, I checked.)

    I'm quite sure that all Unixes invoke signal handlers in some existing thread. So even if errno is thread-local, it needs to be saved and restored.
    OTOH this is really a micro optimization.

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 4, 2010

    Amaury Forgeot d'Arc writes:

    OTOH this is really a micro optimization.

    ["this" = only saving/restoring errno when needed]
    True, but practically nothing is officially safe to do in signal
    handlers, so it's good to avoid code which can be avoided there.
    If one can be bothered to, that is.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 4, 2010

    ["this" = only saving/restoring errno when needed]
    True, but practically nothing is officially safe to do in signal
    handlers, so it's good to avoid code which can be avoided there.
    If one can be bothered to, that is.

    I think it is extremely unlikely that mutating errno in a signal handler is unsafe (after all, the library functions called from that handler can mutate errno too: that's the whole point of the patch IIUC). Adding some configure machinery for this seems unwarranted to me.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 4, 2010

    By the way, I'd like to clear out a potential misunderstanding: the function you are patching doesn't call Python signal handlers in itself (those registered using signal.signal()). It only schedules them for later execution. If you want to save errno around Python signal handlers themselves, PyErr_CheckSignals must be patched as well.

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 5, 2010

    Antoine Pitrou writes:

    By the way, I'd like to clear out a potential misunderstanding: the
    function you are patching doesn't call Python signal handlers in itself
    (those registered using signal.signal()). (...)

    Good point - I'm talking C signal handlers, not Python signal handlers.

    If you want to save errno around Python signal handlers
    themselves, PyErr_CheckSignals must be patched as well.

    Probably not. I don't know Python's errno conventions, if any, but
    it's usually a bug to use errno at any distance from the error. The C
    code near the error can save errno if it wants it later. It can't do
    that around C signal handlers, since those can get called anywhere.

    @hfuru
    Copy link
    Mannequin Author

    hfuru mannequin commented Nov 5, 2010

    Antoine Pitrou writes:

    I think it is extremely unlikely that mutating errno in a signal handler
    is unsafe (after all, the library functions called from that handler can
    mutate errno too: that's the whole point of the patch IIUC). Adding some
    configure machinery for this seems unwarranted to me.

    Fine by me.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 5, 2010

    Ok, fixed in r86214 (3.x), r86215 (3.1) and r86216 (2.7). Thanks for the patch!

    @pitrou pitrou closed this as completed Nov 5, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants