Skip to content

Fix infinite hang #51

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 4 commits into from
Mar 31, 2025
Merged

Conversation

nojaf
Copy link
Collaborator

@nojaf nojaf commented Mar 28, 2025

Hi @pauldambra ,

This is the key part we want to upstream!

It adds a timeout on Dispose/Flush so we avoid infinitely hanging.

Changes:

  • New CTS for the timed out disposal
  • Passing a CT to Write/Flush stream methods
  • Configure the stream to have a write-timeout
  • Refactor of the tests to use a free port instead of having port collision.

@pauldambra pauldambra requested a review from Copilot March 29, 2025 18:44
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces timeouts during disposal and write operations to prevent infinite hangs, while also refactoring cancellation token usage and test setups.

  • Introduces new CancellationTokenSources (_disposingCts and _forceQuitCts) with configurable write and disposal timeouts.
  • Overloads TCPSink and related configuration extensions to support timeout parameters.
  • Refactors tests to use freely bound ports and removes duplicate test implementations.

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Serilog.Sinks.Network/Sinks/UDP/UDPSink.cs Removed redundant _socket = null assignment in Dispose.
Serilog.Sinks.Network/Sinks/TCP/TCPSocketWriter.cs Updated CTS usage, added timeout handling, and refactored reconnection logic.
Serilog.Sinks.Network/Sinks/TCP/TCPSink.cs Altered constructor overloads for improved dependency injection.
Serilog.Sinks.Network/NetworkLoggerConfigurationExtensions.cs Revised parameter types and added timeout parameters.
Test Files Updated tests to use free ports and consolidated UDP/TCP test files for clarity.
ServerPoller.cs Improved asynchronous socket handling and added cancellation support.
PingPonger/Program.cs Updated TCPSink invocations to include timeout parameters.
Files not reviewed (1)
  • Serilog.Sinks.Network/Serilog.Sinks.Network.csproj: Language not supported
Comments suppressed due to low confidence (1)

Serilog.Sinks.Network/Sinks/TCP/TCPSocketWriter.cs:222

  • The null-check for _stream in FlushQueue indicates an unexpected state; consider revisiting the connection lifecycle to ensure _stream is consistently assigned or properly reinitialized.
if (_stream == null)

@nojaf nojaf requested a review from pauldambra March 31, 2025 08:25
@@ -49,6 +49,15 @@ public static LoggerConfiguration TCPSink(
return TCPSink(loggerConfiguration, $"tcp://{ipAddress}:{port}", writeTimeoutMs, disposeTimeoutMs, textFormatter, restrictedToMinimumLevel);
}

public static LoggerConfiguration TCPSink(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thank you!

@pauldambra pauldambra merged commit e35b060 into serilog-contrib:master Mar 31, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants