Skip to content

[FIXED] Connection drain could cause missed reply and/or 100ms delay#915

Merged
kozlovic merged 1 commit into
mainfrom
fix_911
Sep 19, 2025
Merged

[FIXED] Connection drain could cause missed reply and/or 100ms delay#915
kozlovic merged 1 commit into
mainfrom
fix_911

Conversation

@kozlovic

Copy link
Copy Markdown
Member

The more important issue is that we should have drained the internal respMux subscription after all user subscriptions have been drained so that we don't miss replies that would be sent by the user subscriptions callbacks.

Draining the internal respMux subscription would often cause the check for number of subscriptions to be positive, which then would lead to a wait of 100ms before checking again that the number would be down to 0.

This PR addresses both issue: make sure that the internal subscription is drained last, and use condition variable to notify when subscriptions are removed and the number is down to the expected number.

Resolves #911

Signed-off-by: Ivan Kozlovic ivan@synadia.com

The more important issue is that we should have drained the internal
`respMux` subscription after all user subscriptions have been drained
so that we don't miss replies that would be sent by the user subscriptions
callbacks.

Draining the internal `respMux` subscription would often cause the
check for number of subscriptions to be positive, which then would
lead to a wait of 100ms before checking again that the number
would be down to 0.

This PR addresses both issue: make sure that the internal subscription
is drained last, and use condition variable to notify when subscriptions
are removed and the number is down to the expected number.

Resolves #911

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
@codecov

codecov Bot commented Sep 18, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.60317% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.21%. Comparing base (2055642) to head (1b66b87).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/conn.c 74.60% 1 Missing and 15 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #915   +/-   ##
=======================================
  Coverage   70.21%   70.21%           
=======================================
  Files          48       48           
  Lines       17271    17316   +45     
  Branches     3545     3555   +10     
=======================================
+ Hits        12127    12159   +32     
- Misses       1735     1738    +3     
- Partials     3409     3419   +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kozlovic
kozlovic requested a review from mtmk September 18, 2025 22:49

@mtmk mtmk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread src/conn.c
s = natsConn_enqueueUnsubProto(nc, sub->sid);
sid = sub->sid;
natsSub_Unlock(sub);
s = natsConn_enqueueUnsubProto(nc, sid);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

observation: are we taking the risk of unsubbing multiple times since it would be a noop on the server side?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What makes you think we would be UNSUB'ing multiple times? The change here is probably not necessary, but felt like I did not want to have the sub's lock while in the function the enqueue the protocol. The caller has both the connection lock and subMu lock, so there is no risk that this subscription is UNSUB'ed twice here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

makes sense. I was only looking at this change that the natsConn_enqueueUnsubProto is out side the lock now.

@kozlovic
kozlovic merged commit dae1e58 into main Sep 19, 2025
28 checks passed
@kozlovic
kozlovic deleted the fix_911 branch September 19, 2025 13:45
github-actions Bot pushed a commit that referenced this pull request Sep 19, 2025
kozlovic added a commit that referenced this pull request Sep 26, 2025
Release notes will be:

This release contains some breaking changes. See the "Changed" section below.

* Build
  * Disable NATS Streaming by default by @mtmk in #770
  * TLS
    * Require OpenSSL 1.1.1+ to compile. Removed the `NATS_BUILD_TLS_USE_OPENSSL_1_1_API` CMake variable by @kozlovic in #905
    * The option `natsOptions_SetSSLVerificationCallback` signature was changed to replace the use of `SSL_verify_cb` (which required OpenSSL dependency in the `nats.h` file), to the new callback `natsSSLVerifyCb`. See documentation of `natsSSLVerifyCb` to see the cast needed to compile with this new header file by @kozlovic in #908
* Modification of a `natsOptions` object if it has TLS/SSL configuration and is actively used by connections will now return a `NATS_ILLEGAL_STATE` by @kozlovic in #912

* Options
  * Ability to load the trusted CA certificates from a directory using the new option `natsOptions_LoadCATrustedCertificatesPath` by @kerbert101 in #862
  * Ability to connect via HTTP proxy for instance by adding a proxy connection handler using the new option `natsOptions_SetProxyConnHandler` by @wolfkor in #871 and @kozlovic in #897
  * Ability to load the certificate chain and key from a file on every connection attempt using the new option `natsOptions_LoadCertificatesChainDynamic` by @Matus-p in #901
  * Ability to perform concurrent TLS handshakes that may improve time it takes for concurrent connections to be established using the new option `natsOptions_AllowConcurrentTLSHandshakes ` by @kozlovic in #914. Issue was reported by @yanyongcheng in #899
* JetStream
  * Per-message TTL support (a NATS Server v2.11 feature) by @levb in #863
  * Pull consumer priority groups (a NATS Server v2.11 feature) by @levb in #869
* ObjectStore support by @kozlovic in #902. Thanks to @jfflynn41 and @alex1891 for the feedback in #876

* JetStream
  * Handling of publish asynchronous timeouts by @kozlovic in #886. Issue reported by @yanyongcheng in #880
* Timer insertion by @kozlovic in #883. Issue reported by @yanyongcheng in #881

* EventLoop:
  * Handling of possible failure on initial attach by @kozlovic in #918
  * LibEvent: `natsConnection_Close()` not closing the TCP connection by @kozlovic in #882. Issue was reported by @yanyongcheng in #879
  * Libuv: Possible crash if connection is destroyed while receiving data by @kozlovic in #889. Issue was reported by @yanyongcheng in #888
* KeyValue
  * Keys, History or watcher's next may incorrectly return `NATS_TIMEOUT` by @kozlovic in #917/ Issue was reported by @ArashPartow in #916
* MicroServices:
  * Wrong marshaling of `average_processing_time` by @kozlovic in #892. Issue was reported by @Archie3d in #890
  * Statistics error was always incremented by @kozlovic in #894. Issue was reported by @Archie3d in #893
* TLS
  * Unknown type name `SSL_verify_cb` by @kozlovic in #878. Issue was reported by @philipfoulkes in #877
  * Initialization and cleanup code related to OpenSSL was removed since it was deprecated for versions post OpenSSL 1.1. A cleanup function pertinent to 1.1+ code was possibly causing a problem. By @kozlovic in #905. Issue was reported by @vdeters in #904
  * Possible hang during handshake by @kozlovic in #907. Issue was reported by @etrochim in #906
  * Protect calls to `SSL_read` and `SSL_write` with a mutex. Since the same `SSL` object is shared between different threads, the OpenSSL library requires a mutex to be used by @kozlovic in #913
* Memory allocation check by @wooffie in #868
* Add missing status text string by @oldnick85 in #872 and @kozlovic in #874 (the issue was not present in any published release and was introduced in #869)
* Parsing of message headers with `NULL` or all-whitespace values by @habbbe in #873
* Removed some unused code related to handling of responses and added custom inbox with very long prefix test by @kozlovic in #885. Issue was reported by @yanyongcheng in #884
* Connection drain could cause missed reply and/or a 100ms delay by @kozlovic in #915. Issue was reported by @T-Maxxx in #911

* Build
  * Deprecated Ubuntu 20.04 in GitHub actions by @levb in #864
  * Removed the older compiler jobs by @levb in #865
  * Fixed Windows build to use the NATS Server main branch by @levb in #866

* @kerbert101 made their first contribution in #862
* @habbbe made their first contribution in #873
* @wolfkor made their first contribution in #871
* @Matus-p made their first contribution in #901

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
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.

New subscription first outgoing message waiting (warmup)

2 participants