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

[release-1.6] Backport "Try to close race condition in FileWatching tests" #39978

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions stdlib/FileWatching/src/FileWatching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ mutable struct _FDWatcher
end
end

function iswaiting(fwd::_FDWatcher, t::Task)
return fwd.notify.waitq === t.queue
end

mutable struct FDWatcher
watcher::_FDWatcher
readable::Bool
Expand Down Expand Up @@ -653,10 +657,10 @@ function poll_fd(s::Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}},
try
if timeout_s >= 0
result::FDEvent = FDEvent()
timer = Timer(timeout_s) do t
notify(wt)
end
@async begin
t = @async begin
timer = Timer(timeout_s) do t
notify(wt)
end
try
result = wait(fdw, readable=readable, writable=writable)
catch e
Expand All @@ -666,6 +670,12 @@ function poll_fd(s::Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}},
notify(wt)
end
wait(wt)
# It's possible that both the timer and the poll fired on the same
# libuv loop. In that case, which event we see here first depends
# on task schedule order. If we can see that the task isn't waiting
# on the file watcher anymore, just let it finish so we can see
# the modification to `result`
iswaiting(fdw, t) || wait(t)
return result
else
return wait(fdw, readable=readable, writable=writable)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/FileWatching/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Base: uv_error, Experimental
# Writable ends are always tested for write-ability before a write

n = 20
intvls = [2, .2, .1, .005]
intvls = [2, .2, .1, .005, .00001]

pipe_fds = fill((Base.INVALID_OS_HANDLE, Base.INVALID_OS_HANDLE), n)
for i in 1:n
Expand Down