Skip to content

Commit 8c67b3c

Browse files
daipomashiekenhys
committed
winsvc: Fix bug where service accidentally stops after starting (#4954)
**Which issue(s) this PR fixes**: This fixes a bug of #4909. **What this PR does / why we need it**: After 53bcd3c (#4909), the service accidentally stops after starting, without stopping the supervisor and workers. **Docs Changes**: Not needed. **Release Note**: The same as the title. Signed-off-by: Daijiro Fukuda <[email protected]> Co-authored-by: Takuro Ashie <[email protected]> Co-authored-by: Kentaro Hayashi <[email protected]> Signed-off-by: Daijiro Fukuda <[email protected]>
1 parent 6233202 commit 8c67b3c

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.github/workflows/test.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ jobs:
2727
matrix:
2828
os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
2929
ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']
30-
3130
name: Ruby ${{ matrix.ruby-version }} on ${{ matrix.os }}
3231
steps:
3332
- uses: actions/checkout@v3
@@ -43,3 +42,23 @@ jobs:
4342
run: bundle install
4443
- name: Run tests
4544
run: bundle exec rake test TESTOPTS="-v --no-show-detail-immediately"
45+
46+
test-windows-service:
47+
runs-on: windows-latest
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']
52+
name: Windows service (Ruby ${{ matrix.ruby-version }})
53+
steps:
54+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
55+
- name: Set up Ruby
56+
uses: ruby/setup-ruby@e34163cd15f4bb403dcd72d98e295997e6a55798 # v1.238.0
57+
with:
58+
ruby-version: ${{ matrix.ruby-version }}
59+
- name: Install dependencies
60+
run: |
61+
bundle install
62+
rake install
63+
- name: Run tests
64+
run: test\scripts\windows_service_test.ps1

lib/fluent/winsvc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def service_main
6363
loop do
6464
sleep 5
6565
break unless running?
66-
raise Errno::ECHILD unless Process.waitpid2(@pid, Process::WNOHANG)
66+
raise Errno::ECHILD if Process.waitpid(@pid, Process::WNOHANG)
6767
end
6868
rescue Errno::ECHILD
6969
@pid = 0
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
$ErrorActionPreference = "Stop"
2+
Set-PSDebug -Trace 1
3+
4+
$default_conf_path = (Resolve-Path fluent.conf).Path
5+
$current_path = (Get-Location).Path
6+
$log_path = "$current_path/fluentd.log"
7+
8+
ruby bin/fluentd --reg-winsvc i --reg-winsvc-fluentdopt "-c '$default_conf_path' -o '$log_path'"
9+
10+
# Test: must not start automatically
11+
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
12+
Write-Error "The service should not start automatically."
13+
}
14+
15+
Start-Service fluentdwinsvc
16+
Start-Sleep 30
17+
18+
# Test: the service should be running after started
19+
if ((Get-Service fluentdwinsvc).Status -ne "Running") {
20+
Write-Error "The service should be running after started."
21+
}
22+
23+
# Test: no warn/error/fatal logs
24+
Get-ChildItem "*.log" | %{
25+
Get-Content $_
26+
if (Select-String -Path $_ -Pattern "[warn]", "[error]", "[fatal]" -SimpleMatch -Quiet) {
27+
Select-String -Path $_ -Pattern "[warn]", "[error]", "[fatal]" -SimpleMatch
28+
Write-Error "There are abnormal level logs in ${_}:"
29+
}
30+
}
31+
32+
Stop-Service fluentdwinsvc
33+
Start-Sleep 10 # Somehow it is possible that some processes stay alive for a while. (This could be not good behavior...)
34+
35+
# Test: status after stopped
36+
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
37+
Write-Error "The service should be in 'Stopped' status after stopped."
38+
}
39+
# Test: all Ruby processes should stop
40+
$ruby_processes = Get-Process -name ruby -ErrorAction SilentlyContinue
41+
if ($ruby_processes.Count -ne 0) {
42+
Write-Output $ruby_processes
43+
Write-Error "All Ruby processes should stop."
44+
}
45+
46+
# Test: service should stop when the supervisor fails to launch
47+
# https://github.com/fluent/fluentd/pull/4909
48+
$test_setting = @'
49+
<source>
50+
@type sample
51+
@id DUPLICATED_ID
52+
tag test
53+
</source>
54+
<match test>
55+
@type stdout
56+
@id DUPLICATED_ID
57+
</match>
58+
'@
59+
Add-Content -Path "duplicated_id.conf" -Encoding UTF8 -Value $test_setting
60+
ruby bin/fluentd --reg-winsvc-fluentdopt "-c '$current_path/duplicated_id.conf' -o '$log_path'"
61+
Start-Service fluentdwinsvc
62+
Start-Sleep 30
63+
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
64+
Write-Error "The service should be in 'Stopped' status when the supervisor fails to launch."
65+
}
66+
$ruby_processes = Get-Process -name ruby -ErrorAction SilentlyContinue
67+
if ($ruby_processes.Count -ne 0) {
68+
Write-Output $ruby_processes
69+
Write-Error "All Ruby processes should stop."
70+
}
71+
72+
ruby bin/fluentd --reg-winsvc u
73+
Remove-Item $log_path

0 commit comments

Comments
 (0)