Skip to content

time.sleep ignores errors on Windows #85090

Open

Description

@zooba
BPO 40913
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba

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 = 'https://github.com/tjguk'
closed_at = None
created_at = <Date 2020-06-08.18:40:16.499>
labels = ['3.10', 'type-bug', '3.9', 'OS-windows']
title = 'time.sleep ignores errors on Windows'
updated_at = <Date 2020-06-12.21:08:35.755>
user = 'https://github.com/zooba'

bugs.python.org fields:

activity = <Date 2020-06-12.21:08:35.755>
actor = 'steve.dower'
assignee = 'tim.golden'
closed = False
closed_date = None
closer = None
components = ['Windows']
creation = <Date 2020-06-08.18:40:16.499>
creator = 'steve.dower'
dependencies = []
files = []
hgrepos = []
issue_num = 40913
keywords = []
message_count = 8.0
messages = ['371037', '371225', '371235', '371333', '371408', '371409', '371413', '371420']
nosy_count = 5.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40913'
versions = ['Python 3.9', 'Python 3.10']

Activity

  1. zooba commented on Jun 8, 2020

    @zooba
    MemberAuthor

    In time.sleep on Windows, if the WaitForSingleObject call fails, the call returns success. It essentially looks like the timeout was 0.

    Errors should be highly unlikely, as the event object is leaked (see bpo-40912), but if they _do_ occur, we should raise them.

  2. added
    3.7 (EOL)
    3.8 (EOL)
    3.10 (EOL)
    3.9 (EOL)
    type-bug
    on Jun 8, 2020
  3. tjguk commented on Jun 10, 2020

    @tjguk
    Member

    Thinking about testing here.. is there any straightforward way to cause WaitForSingleObjectEx to fail?

    The code change would be fairly slight and amenable to inspection, but it would be good to actually test it ;)

  4. eryksun commented on Jun 10, 2020

    @eryksun
    Contributor

    is there any straightforward way to cause WaitForSingleObjectEx to fail?

    The wait can fail for ERROR_INVALID_HANDLE or ERROR_ACCESS_DENIED (i.e. the handle lacks SYNCHRONIZE access). If _PyOS_SigintEvent were replaced with get/set functions, then a non-waitable handle could be set temporarily via ctypes.

  5. tjguk commented on Jun 12, 2020

    @tjguk
    Member

    Thanks, Eryk. I've had a couple of casts at this (and also with an eye to https://bugs.python.org/issue40912 in a very similar area).

    Trouble is I can't come up with a way of adding a set.. function which doesn't seem wholly artificial, given that it's basically creating an anonymous event and checking its return. I can't come up with a non-testing scenario where the ability to override would be useful.

    If I understand your proposal, a tentative "set..." function would have to take a HANDLE parameter so that it could be overridden by a test? That means its normal use would be something like:

    setSigintEvent(CreateEvent(NULL, ....));

    so either the error checking for that would be inside the function, which feels weird, or would happen outside, which feels like the functions not doing anything. (I appreciate I may be overthinking here).

    I'm very much open to suggestions here, but it seems to me that either:

    We make the (simple) change without tests;

    or we add a setSigintEvent function as above whose only purpose is to be overridden -- and then only for testing.

  6. zooba commented on Jun 12, 2020

    @zooba
    MemberAuthor

    You could test this by getting the event and CloseHandle-ing it. A function to do this could be added to _testcapimodule. It'd have to run in its own process, but we have (a few) helpers around for this.

    Given the concerns, I don't think we should change this in any maintenance releases, so I removed 3.7 and 3.8 from the versions list. I'd be equally happy without an explicit test in

  7. removed
    3.7 (EOL)
    3.8 (EOL)
    on Jun 12, 2020
  8. zooba commented on Jun 12, 2020

    @zooba
    MemberAuthor

    ... equally happy without an explicit test in anything that's only prerelease right now.

  9. eryksun commented on Jun 12, 2020

    @eryksun
    Contributor

    If WaitForSingleObjectEx fails, do you think the system error code should be raised as an OSError? Probably an invalid-handle or access-denied error is too cryptic here. It may be better to raise something like RuntimeError('time.sleep(): invalid SIGINT event handle').

    You could test this by getting the event and CloseHandle-ing it.

    Yes, that's a simpler approach. It should work in a child process that's well controlled.

    I was thinking of a less controlled environment, in which there's a chance that another waitable object will reuse the handle, such as a file object. The test would be reliable regardless of process context (but only in the main thread, of course) if there were a _PyOS_SetSigintEvent function -- or if sigint_event were directly accessible in _testcapimodule. It could just temporarily set a duplicate handle with no access. For example:

        >>> h1 = CreateEvent(None, True, True, None)
        >>> WaitForSingleObject(h1, 0)
        0
        >>> hp = GetCurrentProcess()
        >>> h2 = DuplicateHandle(hp, h1, hp, 0, False, 0)
        >>> WaitForSingleObject(h2, 0)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        pywintypes.error: (5, 'WaitForSingleObject', 'Access is denied.')
  10. zooba commented on Jun 12, 2020

    @zooba
    MemberAuthor

    If WaitForSingleObjectEx fails, do you think the system error code should be raised as an OSError?

    It's invalid (and unfixable) internal state, so perhaps SystemError makes the most sense? There's no point catching it in most cases.

    Or since we don't want to worry too much about formatting the message, why not raise the OSError with the helper function and then immediately chain it with a fixed SystemError?

  11. transferred this issue fromon Apr 10, 2022
  12. added
    extension-modules
    on Nov 29, 2023
  13. aisk commented on Nov 21, 2026

    @aisk
    Member

    This was fixed by GH-28483, both wait paths in time.sleep() on Windows now check for WAIT_FAILED and raise OSError, so I think we can close it now.

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

Metadata

Metadata

Assignees

Labels

3.10 (EOL)3.9 (EOL)OS-windowsextension-modulestype-bug

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions