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

Deadloop in Windows, os.sleep(-1) #23732

Closed
litlighilit opened this issue Jun 18, 2024 · 5 comments · Fixed by #23734
Closed

Deadloop in Windows, os.sleep(-1) #23732

litlighilit opened this issue Jun 18, 2024 · 5 comments · Fixed by #23734

Comments

@litlighilit
Copy link
Contributor

litlighilit commented Jun 18, 2024

Description

when defined(windows):
  import std/os
  sleep(-1)  # <- deadloop

BTW, on POSIX, it just returns immediately, as sleep calls nanosleep, which exits with errno set as EINVAL for negative values.

Nim Version

At least from 1.6.14 to 2.1.1 and devel

Current Output

No response

Expected Output

No response

Possible Solution

Two direction:

  1. raises a exception on all platforms when milsecs is negative.
  2. add check in Windows, returns immediately for negative values

Additional Information

In Python, time.sleep raises ValueError on negative value.

Maybe we can raises OSError as Nim's sleep is in std/os if the first direction is accepted.

On the other hand, golang's time.Sleep will return immediately for negative values.

@litlighilit
Copy link
Contributor Author

litlighilit commented Jun 18, 2024

BTW, It seems to deadloop as os.sleep calls Sleep on Windows, whose parameter type, is in fact DWORD, an uint32, but Nim wraps it as int32, so pass negative int just becomes a large unsigned int.

Still a DWORD relative issue...

@Araq
Copy link
Member

Araq commented Jun 18, 2024

What's wrong with -1 meaning "forever"?

@litlighilit
Copy link
Contributor Author

litlighilit commented Jun 18, 2024

on posix, it returns immediately. (and set errno to EINVAL)

@litlighilit
Copy link
Contributor Author

litlighilit commented Jun 18, 2024

What's wrong with -1 meaning "forever"?

And strictly speaking, not forever, but a very large uint amount of time.

e.g. Currently on Windows:
sleep(-1) in fact does Sleep(4294967295)

a.k.a. while sleep(-1) means sleep forever is somewhat reasonable, it means sleep for 49 days on Windows and sleep for no time on POSIX.

@juancarlospaco
Copy link
Collaborator

sleep should take Natural.

@Araq Araq closed this as completed in 2a658c6 Jun 18, 2024
narimiran pushed a commit that referenced this issue Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants