From ee5b7a38ce24f92b6e783a0e8cddceb605b43eb8 Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 26 Sep 2024 14:07:33 +0800 Subject: [PATCH 1/3] Fixed issue: throw IOException: The process cannot access the file 'xxxx\log\Non-Session-Log.messages.current.log' because it is being used by another process. --- QuickFIXn/Logger/NonSessionLog.cs | 4 +++- QuickFIXn/ThreadedSocketAcceptor.cs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/QuickFIXn/Logger/NonSessionLog.cs b/QuickFIXn/Logger/NonSessionLog.cs index 3cc0d6bc9..7393318f3 100644 --- a/QuickFIXn/Logger/NonSessionLog.cs +++ b/QuickFIXn/Logger/NonSessionLog.cs @@ -4,7 +4,7 @@ namespace QuickFix.Logger; /// A logger that can be used when the calling logic cannot identify a session (which is rare). /// Does not create a file until first write. /// -public class NonSessionLog { +public class NonSessionLog : System.IDisposable { private readonly ILogFactory _logFactory; private ILog? _log; @@ -21,5 +21,7 @@ internal void OnEvent(string s) { } _log.OnEvent(s); } + + public void Dispose() => _log?.Dispose(); } diff --git a/QuickFIXn/ThreadedSocketAcceptor.cs b/QuickFIXn/ThreadedSocketAcceptor.cs index c57ce7950..9a3f0275b 100755 --- a/QuickFIXn/ThreadedSocketAcceptor.cs +++ b/QuickFIXn/ThreadedSocketAcceptor.cs @@ -274,6 +274,8 @@ public void Stop(bool force) LogoutAllSessions(force); DisposeSessions(); _sessions.Clear(); + _nonSessionLog.Dispose(); + _isStarted = false; // FIXME StopSessionTimer(); // FIXME Session.UnregisterSessions(GetSessions()); From 22fb3d6d326a29bc8caeda288bfd3ef6d54c615a Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 26 Sep 2024 14:44:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Since=20.NET=20runtime=E2=80=99s=20exceptio?= =?UTF-8?q?ns=20use=20culture=20information=20to=20set=20the=20Message=20v?= =?UTF-8?q?alue,=20the=20message=20value=20of=20the=20Exception=20should?= =?UTF-8?q?=20not=20be=20asserted,=20but=20rather=20the=20ErrorCode=20of?= =?UTF-8?q?=20the=20Exception.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UnitTests/ThreadedSocketReactorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/ThreadedSocketReactorTests.cs b/UnitTests/ThreadedSocketReactorTests.cs index 57c300b99..f78234568 100644 --- a/UnitTests/ThreadedSocketReactorTests.cs +++ b/UnitTests/ThreadedSocketReactorTests.cs @@ -57,7 +57,7 @@ public void TestStartOnBusyPort() var ex = Assert.Throws(delegate { testingObject.Run(); })!; StringAssert.StartsWith(" Error starting listener:", stdOut.ToString()); - StringAssert.StartsWith("Address already in use", ex.Message); + Assert.That(ex.SocketErrorCode, Is.EqualTo(SocketError.AddressAlreadyInUse)); } [TearDown] From b5b7a9c6aa205e79c6a3a73558b79634142874ba Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 10 Oct 2024 16:56:36 +0800 Subject: [PATCH 3/3] Update NonSessionLog.cs. The implementation ensures that once the NonSessionLog instance has been released, there are no side effects even if the OnEvent function is called unexpectedly. --- QuickFIXn/Logger/NonSessionLog.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/QuickFIXn/Logger/NonSessionLog.cs b/QuickFIXn/Logger/NonSessionLog.cs index 7393318f3..5e369c863 100644 --- a/QuickFIXn/Logger/NonSessionLog.cs +++ b/QuickFIXn/Logger/NonSessionLog.cs @@ -16,12 +16,23 @@ internal NonSessionLog(ILogFactory logFactory) { } internal void OnEvent(string s) { + if (_disposed) return; + lock (_sync) { _log ??= _logFactory.CreateNonSessionLog(); } - _log.OnEvent(s); + _log?.OnEvent(s); } - public void Dispose() => _log?.Dispose(); + private bool _disposed; + public void Dispose() { + if (_disposed) return; + + if (_log != null) { + _log.Dispose(); + _log = null; + _disposed = true; + } + } }