Skip to content

Commit e96728c

Browse files
authored
FileWatching: cleanup timers on return (#36301)
Following my own advice at #36217. Avoids a minor temporary resource leak.
1 parent ee0621f commit e96728c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

stdlib/FileWatching/src/FileWatching.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,13 @@ giving the result of the polling.
649649
function poll_fd(s::Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}}, timeout_s::Real=-1; readable=false, writable=false)
650650
wt = Condition()
651651
fdw = _FDWatcher(s, readable, writable)
652+
local timer
652653
try
653654
if timeout_s >= 0
654655
result::FDEvent = FDEvent()
655-
@async (sleep(timeout_s); notify(wt))
656+
timer = Timer(timeout_s) do t
657+
notify(wt)
658+
end
656659
@async begin
657660
try
658661
result = wait(fdw, readable=readable, writable=writable)
@@ -669,6 +672,7 @@ function poll_fd(s::Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}},
669672
end
670673
finally
671674
close(fdw, readable, writable)
675+
@isdefined(timer) && close(timer)
672676
end
673677
end
674678

@@ -686,13 +690,17 @@ This behavior of this function varies slightly across platforms. See
686690
"""
687691
function watch_file(s::AbstractString, timeout_s::Real=-1)
688692
fm = FileMonitor(s)
693+
local timer
689694
try
690695
if timeout_s >= 0
691-
@async (sleep(timeout_s); close(fm))
696+
timer = Timer(timeout_s) do t
697+
close(fm)
698+
end
692699
end
693700
return wait(fm)
694701
finally
695702
close(fm)
703+
@isdefined(timer) && close(timer)
696704
end
697705
end
698706

@@ -730,14 +738,17 @@ function watch_folder(s::String, timeout_s::Real=-1)
730738
# create a second monitor object just for that purpose.
731739
# We still take the events from the primary stream.
732740
fm2 = FileMonitor(s)
741+
timer = Timer(timeout_s) do t
742+
close(fm2)
743+
end
733744
try
734-
@async (sleep(timeout_s); close(fm2))
735745
while isopen(fm.notify) && !isready(fm.notify)
736746
fm2.handle == C_NULL && return "" => FileEvent() # timeout
737747
wait(fm2)
738748
end
739749
finally
740750
close(fm2)
751+
close(timer)
741752
end
742753
# guaranteed that next call to `wait(fm)` is non-blocking
743754
# since we haven't entered the libuv event loop yet
@@ -783,9 +794,12 @@ it is more reliable and efficient, although in some situations it may not be ava
783794
"""
784795
function poll_file(s::AbstractString, interval_seconds::Real=5.007, timeout_s::Real=-1)
785796
pfw = PollingFileWatcher(s, Float64(interval_seconds))
797+
local timer
786798
try
787799
if timeout_s >= 0
788-
@async (sleep(timeout_s); close(pfw))
800+
timer = Timer(timeout_s) do t
801+
close(pfw)
802+
end
789803
end
790804
statdiff = wait(pfw)
791805
if isa(statdiff[2], IOError)
@@ -795,6 +809,7 @@ function poll_file(s::AbstractString, interval_seconds::Real=5.007, timeout_s::R
795809
return statdiff
796810
finally
797811
close(pfw)
812+
@isdefined(timer) && close(timer)
798813
end
799814
end
800815

0 commit comments

Comments
 (0)