Skip to content

Commit b973802

Browse files
kenhysdaipom
andauthored
Backport(v1.16) Windows: Fix an issue where stopping the service immediately after startup could leave the processes (#4782) (#4802)
**Which issue(s) this PR fixes**: Backport #4782 **What this PR does / why we need it**: Add retry for stop event for Windows Service to fix #3937. If `Event.open()` ([OpenEvent](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-openeventw)) is called before the `Event.new()` ([CreateEvent](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventw)), `Event.open()` raises `Errno::ENOENT`. This causes the service to be stopped while the supervisor and worker process remains. It causes #3937. This PR fixes it by adding retry. https://github.com/fluent/fluentd/blob/30c3ce00ff165b1b5d9f53fc0a027074bbcab0da/lib/fluent/winsvc.rb#L90 https://github.com/fluent/fluentd/blob/30c3ce00ff165b1b5d9f53fc0a027074bbcab0da/lib/fluent/supervisor.rb#L299 **Docs Changes**: Not needed. **Release Note**: It would be good to have both of the following. * Windows: Fixed an issue where stopping the service immediately after startup could leave the processes. * Windows: Fixed an issue where stopping service sometimes can not be completed forever. Signed-off-by: Daijiro Fukuda <[email protected]> Signed-off-by: Kentaro Hayashi <[email protected]> Co-authored-by: Daijiro Fukuda <[email protected]>
1 parent 8e303cf commit b973802

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

lib/fluent/winsvc.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ def service_main
6363
end
6464

6565
def service_stop
66-
set_event(@service_name)
67-
if @pid > 0
68-
Process.waitpid(@pid)
66+
if @pid <= 0
67+
set_event(@service_name)
68+
return
6969
end
70+
71+
wait_supervisor_finished
7072
end
7173

7274
def service_paramchange
@@ -91,6 +93,29 @@ def set_event(event_name)
9193
ev.set
9294
ev.close
9395
end
96+
97+
def repeat_set_event_several_times_until_success(event_name)
98+
retries = 0
99+
max_retries = 10
100+
delay_sec = 3
101+
102+
begin
103+
set_event(event_name)
104+
rescue Errno::ENOENT
105+
# This error occurs when the supervisor process has not yet created the event.
106+
# If STOP is immediately executed, this state will occur.
107+
# Retry `set_event' to wait for the initialization of the supervisor.
108+
retries += 1
109+
raise if max_retries < retries
110+
sleep(delay_sec)
111+
retry
112+
end
113+
end
114+
115+
def wait_supervisor_finished
116+
repeat_set_event_several_times_until_success(@service_name)
117+
Process.waitpid(@pid)
118+
end
94119
end
95120

96121
FluentdService.new(opts[:service_name]).mainloop

0 commit comments

Comments
 (0)