From 7ecaa5f40a902127cdaeb5b5948db43c66ed414a Mon Sep 17 00:00:00 2001 From: Thierry Matthey Date: Tue, 4 Jun 2024 18:29:35 +0200 Subject: [PATCH] Catch potential exceptions thrown in YPipe TryRead --- src/NetMQ/Core/YPipe.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/NetMQ/Core/YPipe.cs b/src/NetMQ/Core/YPipe.cs index 61f002df6..61fc807c3 100644 --- a/src/NetMQ/Core/YPipe.cs +++ b/src/NetMQ/Core/YPipe.cs @@ -180,17 +180,25 @@ public bool CheckRead() /// true if the read succeeded, otherwise false. public bool TryRead([MaybeNullWhen(returnValue: false)] out T value) { - // Try to prefetch a value. - if (!CheckRead()) + try + { + // Try to prefetch a value. + if (!CheckRead()) + { + value = default(T); + return false; + } + + // There was at least one value prefetched. + // Return it to the caller. + value = m_queue.Pop(); + return true; + } + catch { value = default(T); return false; } - - // There was at least one value prefetched. - // Return it to the caller. - value = m_queue.Pop(); - return true; } ///