# How can \`sleep(0.001)\` error?

**URL:** https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394
**Category:** General Usage
**Tags:** question
**Created:** [April 23, 2024, 1:38pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394 "2024-04-23T13:38:38Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [April 23, 2024, 1:38pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/1 "2024-04-23T13:38:38Z")

</div>

Hi all 🙂

How can `sleep(0.001)` error?

```julia
┌ Error:
│ EOFError: read end of file
│ Stacktrace:
│ [1] wait
│ @ .\asyncevent.jl:159 [inlined]
│ [2] sleep
│ @ .\asyncevent.jl:265 [inlined]

```

This happend on Windows. Julia v10.0.0

Thanks all!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 23, 2024, 1:40pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/2 "2024-04-23T13:40:07Z")

</div>

Please post a minimal working snippet of code that triggers this error for you. [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [April 23, 2024, 2:00pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/3 "2024-04-23T14:00:16Z")

</div>

Hard to give a minimal working snippet. Just can tell that it happens almost never (my feeling 1 in 10^15).  
The surrounding code is a `while-true` with a inner `try-catch` and a pulling `ccall`. All this runs in a separate task.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [April 23, 2024, 2:14pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/4 "2024-04-23T14:14:53Z")

</div>

> [@SunnyUser](#):
>
> Hard to give a minimal working snippet.

It shouldn’t be hard to share the code you’re actually trying to run. You only showed a partial error message.

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [April 23, 2024, 2:26pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/5 "2024-04-23T14:26:30Z")

</div>

I can do that… But I have to remove company code. But the following function gets spawned in at task.

```julia
function get_next_item()
    while _IS_RUNNING[]
        try

            return_buffer = managed_allocating_ccall(
                ccall_get_next_item
            )

            if isnothing(return_buffer)
                sleep(0.001)
                continue
            end

            distribute_to_channels(
                deserialize_item(return_buffer)...
            )
        catch e 
            @error(
                sprint(showerror, e, catch_backtrace())
            )
        end
    end
end

```

and

```julia
function managed_allocating_ccall(allocating_ccall::Function)::Union{IOBuffer, Nothing}
    (return_value_ptr, return_value_length) = allocating_ccall()
        
    if return_value_ptr == Ptr{UInt8}()
        return nothing
    end

    return_value_length_buffer = IOBuffer(return_value_length)
    return_value_length = read(return_value_length_buffer, Int32)
    return_value = unsafe_wrap(Vector{UInt8}, return_value_ptr, return_value_length)

    return_value = deepcopy(return_value)
    ccall_free(return_value_ptr)

    return IOBuffer(return_value)
end

```

and

```julia
function ccall_get_next_item()
    return_value_length = zeros(UInt8, 4)

    return_value_ptr = @my_semaphore_lock begin
        GC.@preserve return_value_length begin
            @ccall $C_ABI_POINTER_GET_NEXT_ITEM(
                pointer(return_value_length)::Ptr{UInt8},
            )::Ptr{UInt8}
        end
    end with Semaphore _CCALL_SEMAPHORE

    (return_value_ptr, return_value_length)
end

```

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [June 14, 2024, 11:31am UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/6 "2024-06-14T11:31:21Z")

</div>

I got this error again in production. I have no idea to debug or fix it. Can me help anybody?

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 14, 2024, 11:45am UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/7 "2024-06-14T11:45:59Z")

</div>

Wrap `sleep` in a try-catch block and ignore `EOFError`:

```julia
try
    sleep(0.001)
catch ex
    ex isa EOFError || rethrow()
end

```

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [June 14, 2024, 11:53am UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/8 "2024-06-14T11:53:55Z")

</div>

Thank you for that idea. But why isn’t that in Base or why does it happen at all? I mean I don’t want to wrap the sleep method all the time

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 14, 2024, 12:10pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/9 "2024-06-14T12:10:21Z")

</div>

This should get a better error message. The error is thrown here:

> <https://github.com/JuliaLang/julia/blob/222231f047462bcb2cef3e402c86cf41df82414d/base/asyncevent.jl#L159>

Which triggers in this case:

> <https://github.com/JuliaLang/julia/blob/222231f047462bcb2cef3e402c86cf41df82414d/base/asyncevent.jl#L122>

So this throws `EOFError` if the timer has already expired i.e. the sleeping duration is too short.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [June 14, 2024, 12:39pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/10 "2024-06-14T12:39:11Z")

</div>

It seems like for `sleep` the code should be modified so no error is thrown in that case. It doesn’t seem helpful to error if it “overslept”, in fact that seems expected in some cases.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 14, 2024, 1:04pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/11 "2024-06-14T13:04:50Z")

</div>

> [@ericphanson](#):
>
> It seems like for `sleep` the code should be modified so no error is thrown in that case. It doesn’t seem helpful to error if it “overslept”, in fact that seems expected in some cases.

> <https://github.com/JuliaLang/julia/pull/54801>
>
> This prevents \`sleep\` from throwing an \`EOFError\` under rare conditions (with ve…ry short timer durations) where the \`Timer\` object is already closed before it can be signaled, which seems like a bug.
> 
> See \[the discourse discussion\](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394).

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [June 27, 2024, 11:59am UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/12 "2024-06-27T11:59:04Z")

</div>

If the issue was the duration being too short, we would expect the following code to throw an error as the timer `t` has already expired by the time we wait for it. But it does not.

```julia
julia> t = Timer(1); sleep(2); wait(t)

julia>

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 27, 2024, 1:10pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/13 "2024-06-27T13:10:38Z")

</div>

> [@SunnyUser](#):
>
> If the issue was the duration being too short, we would expect the following code to throw an error as the timer `t` has already expired by the time we wait for it. But it does not.

`wait` on a `Timer` is guaranteed not to return until the `Timer` is expired and the `Timer` has not yet told anyone that it has expired (or its been told that it has expired through `close`). In other words, if `t` has already expired when the first `wait` call occurs, `wait` just notes that down atomically and returns immediately. Subsequent calls to `wait` on the same object throw, since the timer has already notified the program that it has expired:

```julia
julia> t = Timer(1); sleep(2);

julia> isopen(t)
false

julia> wait(t) # first time someone waits on this specific Timer

julia> wait(t) # second time someone waits on this specific Timer
ERROR: EOFError: read end of file
Stacktrace:
 [1] wait(t::Timer)
   @ Base ./asyncevent.jl:159
 [2] top-level scope
   @ REPL[16]:1

```

The error message is maximally unhelpful here, and I agree that this should throw on the first `wait` too. As I understand it, this behavior is part of the buggy behavior mentioned [in the linked PR](https://github.com/JuliaLang/julia/pull/54801#issuecomment-2171994543).

---

<div class="post-metadata">

### Author: ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)
#### Post date: [June 27, 2024, 1:12pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/14 "2024-06-27T13:12:51Z")

</div>

> <https://github.com/JuliaLang/julia/pull/54955>
>
> We will no longer throw a error if the timer fires after we first checked set.
> …
> But still keeps this case functional:
> \`\`\`
> julia\> t = Timer(1000000000);
> 
> julia\> close(t)
> 
> julia\> t.set
> false
> 
> julia\> wait(t)
> ERROR: EOFError: read end of file
> Stacktrace:
> \[1\] wait(t::Timer)
> @ Base .\\asyncevent.jl:159
> \[2\] top-level scope
> @ REPL\[16\]:1
> \`\`\`

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 27, 2024, 1:58pm UTC](https://discourse.julialang.org/t/how-can-sleep-0-001-error/113394/15 "2024-06-27T13:58:29Z")

</div>

The sleep function is not really intended for a duration of 1ms. If you need to sleep for less than 5 ms, try using sleep\_ms()` from the Timers.jl package. Or use Libc.systemsleep() which is still better than sleep() .
