Skip to content

Backport(v1.16): winsvc: Fix bug where service accidentally stops after starting (#4954) #4955

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

Merged
merged 1 commit into from
May 14, 2025
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
21 changes: 20 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
matrix:
os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']

name: Ruby ${{ matrix.ruby-version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -43,3 +42,23 @@ jobs:
run: bundle install
- name: Run tests
run: bundle exec rake test TESTOPTS="-v --no-show-detail-immediately"

test-windows-service:
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']
name: Windows service (Ruby ${{ matrix.ruby-version }})
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Ruby
uses: ruby/setup-ruby@e34163cd15f4bb403dcd72d98e295997e6a55798 # v1.238.0
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: |
bundle install
rake install
- name: Run tests
run: test\scripts\windows_service_test.ps1
2 changes: 1 addition & 1 deletion lib/fluent/winsvc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def service_main
loop do
sleep 5
break unless running?
raise Errno::ECHILD unless Process.waitpid2(@pid, Process::WNOHANG)
raise Errno::ECHILD if Process.waitpid(@pid, Process::WNOHANG)
end
rescue Errno::ECHILD
@pid = 0
Expand Down
73 changes: 73 additions & 0 deletions test/scripts/windows_service_test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
$ErrorActionPreference = "Stop"
Set-PSDebug -Trace 1

$default_conf_path = (Resolve-Path fluent.conf).Path
$current_path = (Get-Location).Path
$log_path = "$current_path/fluentd.log"

ruby bin/fluentd --reg-winsvc i --reg-winsvc-fluentdopt "-c '$default_conf_path' -o '$log_path'"

# Test: must not start automatically
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
Write-Error "The service should not start automatically."
}

Start-Service fluentdwinsvc
Start-Sleep 30

# Test: the service should be running after started
if ((Get-Service fluentdwinsvc).Status -ne "Running") {
Write-Error "The service should be running after started."
}

# Test: no warn/error/fatal logs
Get-ChildItem "*.log" | %{
Get-Content $_
if (Select-String -Path $_ -Pattern "[warn]", "[error]", "[fatal]" -SimpleMatch -Quiet) {
Select-String -Path $_ -Pattern "[warn]", "[error]", "[fatal]" -SimpleMatch
Write-Error "There are abnormal level logs in ${_}:"
}
}

Stop-Service fluentdwinsvc
Start-Sleep 10 # Somehow it is possible that some processes stay alive for a while. (This could be not good behavior...)

# Test: status after stopped
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
Write-Error "The service should be in 'Stopped' status after stopped."
}
# Test: all Ruby processes should stop
$ruby_processes = Get-Process -name ruby -ErrorAction SilentlyContinue
if ($ruby_processes.Count -ne 0) {
Write-Output $ruby_processes
Write-Error "All Ruby processes should stop."
}

# Test: service should stop when the supervisor fails to launch
# https://github.com/fluent/fluentd/pull/4909
$test_setting = @'
<source>
@type sample
@id DUPLICATED_ID
tag test
</source>
<match test>
@type stdout
@id DUPLICATED_ID
</match>
'@
Add-Content -Path "duplicated_id.conf" -Encoding UTF8 -Value $test_setting
ruby bin/fluentd --reg-winsvc-fluentdopt "-c '$current_path/duplicated_id.conf' -o '$log_path'"
Start-Service fluentdwinsvc
Start-Sleep 30
if ((Get-Service fluentdwinsvc).Status -ne "Stopped") {
Write-Error "The service should be in 'Stopped' status when the supervisor fails to launch."
}
$ruby_processes = Get-Process -name ruby -ErrorAction SilentlyContinue
if ($ruby_processes.Count -ne 0) {
Write-Output $ruby_processes
Write-Error "All Ruby processes should stop."
}

ruby bin/fluentd --reg-winsvc u
Remove-Item $log_path
Loading