@@ -649,10 +649,13 @@ giving the result of the polling.
649
649
function poll_fd (s:: Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}} , timeout_s:: Real = - 1 ; readable= false , writable= false )
650
650
wt = Condition ()
651
651
fdw = _FDWatcher (s, readable, writable)
652
+ local timer
652
653
try
653
654
if timeout_s >= 0
654
655
result:: FDEvent = FDEvent ()
655
- @async (sleep (timeout_s); notify (wt))
656
+ timer = Timer (timeout_s) do t
657
+ notify (wt)
658
+ end
656
659
@async begin
657
660
try
658
661
result = wait (fdw, readable= readable, writable= writable)
@@ -669,6 +672,7 @@ function poll_fd(s::Union{RawFD, Sys.iswindows() ? WindowsRawSocket : Union{}},
669
672
end
670
673
finally
671
674
close (fdw, readable, writable)
675
+ @isdefined (timer) && close (timer)
672
676
end
673
677
end
674
678
@@ -686,13 +690,17 @@ This behavior of this function varies slightly across platforms. See
686
690
"""
687
691
function watch_file (s:: AbstractString , timeout_s:: Real = - 1 )
688
692
fm = FileMonitor (s)
693
+ local timer
689
694
try
690
695
if timeout_s >= 0
691
- @async (sleep (timeout_s); close (fm))
696
+ timer = Timer (timeout_s) do t
697
+ close (fm)
698
+ end
692
699
end
693
700
return wait (fm)
694
701
finally
695
702
close (fm)
703
+ @isdefined (timer) && close (timer)
696
704
end
697
705
end
698
706
@@ -730,14 +738,17 @@ function watch_folder(s::String, timeout_s::Real=-1)
730
738
# create a second monitor object just for that purpose.
731
739
# We still take the events from the primary stream.
732
740
fm2 = FileMonitor (s)
741
+ timer = Timer (timeout_s) do t
742
+ close (fm2)
743
+ end
733
744
try
734
- @async (sleep (timeout_s); close (fm2))
735
745
while isopen (fm. notify) && ! isready (fm. notify)
736
746
fm2. handle == C_NULL && return " " => FileEvent () # timeout
737
747
wait (fm2)
738
748
end
739
749
finally
740
750
close (fm2)
751
+ close (timer)
741
752
end
742
753
# guaranteed that next call to `wait(fm)` is non-blocking
743
754
# 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
783
794
"""
784
795
function poll_file (s:: AbstractString , interval_seconds:: Real = 5.007 , timeout_s:: Real = - 1 )
785
796
pfw = PollingFileWatcher (s, Float64 (interval_seconds))
797
+ local timer
786
798
try
787
799
if timeout_s >= 0
788
- @async (sleep (timeout_s); close (pfw))
800
+ timer = Timer (timeout_s) do t
801
+ close (pfw)
802
+ end
789
803
end
790
804
statdiff = wait (pfw)
791
805
if isa (statdiff[2 ], IOError)
@@ -795,6 +809,7 @@ function poll_file(s::AbstractString, interval_seconds::Real=5.007, timeout_s::R
795
809
return statdiff
796
810
finally
797
811
close (pfw)
812
+ @isdefined (timer) && close (timer)
798
813
end
799
814
end
800
815
0 commit comments