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

time.sleep(-1.0) behaviour #56668

Closed
eckhardt mannequin opened this issue Jul 1, 2011 · 8 comments
Closed

time.sleep(-1.0) behaviour #56668

eckhardt mannequin opened this issue Jul 1, 2011 · 8 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@eckhardt
Copy link
Mannequin

eckhardt mannequin commented Jul 1, 2011

BPO 12459
Nosy @rhettinger, @jcea, @pitrou, @vstinner, @giampaolo, @durban
Files
  • sleep_negative.patch
  • 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/rhettinger'
    closed_at = <Date 2011-07-05.20:01:45.931>
    created_at = <Date 2011-07-01.06:45:50.392>
    labels = ['extension-modules', 'type-bug']
    title = 'time.sleep(-1.0) behaviour'
    updated_at = <Date 2011-07-05.20:01:45.929>
    user = 'https://bugs.python.org/eckhardt'

    bugs.python.org fields:

    activity = <Date 2011-07-05.20:01:45.929>
    actor = 'vstinner'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2011-07-05.20:01:45.931>
    closer = 'vstinner'
    components = ['Extension Modules']
    creation = <Date 2011-07-01.06:45:50.392>
    creator = 'eckhardt'
    dependencies = []
    files = ['22533']
    hgrepos = []
    issue_num = 12459
    keywords = ['patch']
    message_count = 8.0
    messages = ['139548', '139558', '139559', '139560', '139563', '139573', '139901', '139902']
    nosy_count = 9.0
    nosy_names = ['rhettinger', 'jcea', 'pitrou', 'vstinner', 'giampaolo.rodola', 'eckhardt', 'daniel.urban', 'santoso.wijaya', 'python-dev']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12459'
    versions = ['Python 3.3']

    @eckhardt
    Copy link
    Mannequin Author

    eckhardt mannequin commented Jul 1, 2011

    For reference, see the thread on the users' mailinglist/newsgroup from 2011-06-29 "how to call a function for evry 10 seconds" and the thread on the developers' mailinglist from 2011-06-30 "time.sleep(-1) behaviour".

    The problem is how negative arguments to time.sleep() are handled. Python 2.x (tested 2.5 and 2.7) implementations on MS Windows seems to have a 32-bit underflow while converting the given argument to the DWORD that is passed to win32's Sleep() function. This causes a small negative value to sleep for a long time.

    On Linux, using Python 2.6, you get an IOError instead. While an error is better than blocking effectively forever, the use of an IOError to signal the wrong argument is at least confusing. I guess it is an artifact of the implementation, but that shouldn't be visible prominently.

    IMHH, both versions should raise a ValueError to signal invalid arguments. However, there was also the suggestion to simply treat negative values as zero, after all time.sleep() is already documented to possibly sleep longer than specified.

    @eckhardt eckhardt mannequin added the type-bug An unexpected behavior, bug, or error label Jul 1, 2011
    @rhettinger rhettinger self-assigned this Jul 1, 2011
    @vstinner
    Copy link
    Member

    vstinner commented Jul 1, 2011

    I think that time.sleep() should behave as select.select() (issue bpo-11757, commit 3982be773b54) and signal.sigtimedwait(): raise a ValueError if the timeout is negative. A good reason to always raise an error is that floatsleep() has different implementations. Especially, the select() implementation behaves differently depending on the platform: negative timeout raises an error (select.error(22, 'Invalid argument')) or returns immediatly.

    Attached patch raises an error if the time length is negative. It avoids the integer overflow in the Windows implementation.

    @pitrou
    Copy link
    Member

    pitrou commented Jul 1, 2011

    I agree with the ValueError suggestion. Since it would slightly change existing behaviour, it can only be done in a feature release (3.3).

    According to Google Code Search, deliberate uses of sleep(-1) are almost non-existent:
    http://www.google.com/codesearch#search/&q=%22sleep%28-1%29%22%20lang:python&type=cs

    @pitrou pitrou added the extension-modules C modules in the Modules dir label Jul 1, 2011
    @vstinner
    Copy link
    Member

    vstinner commented Jul 1, 2011

    According to Google Code Search, deliberate uses of sleep(-1)
    are almost non-existent:

    The search gives two results, in pycaf and a plone installer (in "compilezpy.py"). I don't know what is the expected behaviour: "infinite" sleep? wait for a SIGINT signal? I'm ok to only change this in Python 3.3, it's not a good idea to introduce subtle differences in minor Python releases.

    @vstinner
    Copy link
    Member

    vstinner commented Jul 1, 2011

    See also bpo-12462, I found something weird in the signal handling of floatsleep().

    @vstinner
    Copy link
    Member

    vstinner commented Jul 1, 2011

    Tim Lesher on python-dev: "On the Windows side, Sleep(-1) as "infinite" is correct and documented:
    http://msdn.microsoft.com/en-us/library/ms686298(v=vs.85).aspx"

    Wine defines INFINITE using "#define INFINITE 0xFFFFFFFF":
    http://source.winehq.org/source/include/winbase.h

    -1 becomes INFINITE because of an integer overflow (because of a cast from double to *unsigned* long). time.sleep(-2) doesn't sleep for an infinite time, but for more than 136 years (maybe more in 64 bits?):

    >>> print(datetime.timedelta(seconds=-2 & 0xFFFFFFFF))
    49710 days, 6:28:14

    What is the usecase of Sleep(INFINITE)? Wait a signal or wait another event?

    Because other platforms don't support the INFINITE timeout, I suggest to always raise an error to have the same behaviour on all platforms.

    On POSIX, you can use pause(), sigwait(), select() or other functions to wait a signal for example. If we something something like that on Windows, we need a new function, but not a magic time.sleep(-1) please.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 5, 2011

    New changeset 0e5485634817 by Victor Stinner in branch 'default':
    Issue bpo-12459: time.sleep() now raises a ValueError if the sleep length is
    http://hg.python.org/cpython/rev/0e5485634817

    @vstinner
    Copy link
    Member

    vstinner commented Jul 5, 2011

    Tim Lesher agreed to raise an exception ("That makes sense. Better to be consistent within the time API--I know the different semantics of time.clock() have confused people around here."), so I think that everybody agreed to raise an exception.

    I commited my commit, let close this issue.

    @vstinner vstinner closed this as completed Jul 5, 2011
    @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
    extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants