Repository navigation
[C++] Handle exception in creating socket when fd limit is reached - #14587
Merged
Merged
Conversation
merlimat
requested review from
BewareMyPower,
addisonj,
codelipenghui,
massakam and
rdhabalia
March 7, 2022 20:31
|
@merlimat:Thanks for your contribution. For this PR, do we need to update docs? |
BewareMyPower
left a comment
Contributor
There was a problem hiding this comment.
Should we catch the exception in ConnectionPool:: getConnectionAsync instead of inside the constructor?
pulsar/pulsar-client-cpp/lib/ConnectionPool.cc
Lines 93 to 94 in 95c1581
Because if the socket object failed to construct, the ClientConnection object will still be inserted into the connection pool.
Co-authored-by: Yunze Xu <[email protected]>
BewareMyPower
approved these changes
Mar 9, 2022
BewareMyPower
added a commit
to BewareMyPower/pulsar
that referenced
this pull request
Mar 24, 2022
### Motivation apache#14823 fixes the flaky `testConnectTimeout` but it's also a regression of apache#14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`.
BewareMyPower
added a commit
that referenced
this pull request
Mar 24, 2022
### Motivation #14823 fixes the flaky `testConnectTimeout` but it's also a regression of #14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`.
BewareMyPower
added a commit
that referenced
this pull request
Mar 24, 2022
### Motivation #14823 fixes the flaky `testConnectTimeout` but it's also a regression of #14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`. (cherry picked from commit 54c368e)
BewareMyPower
added a commit
that referenced
this pull request
Mar 24, 2022
### Motivation #14823 fixes the flaky `testConnectTimeout` but it's also a regression of #14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`. (cherry picked from commit 54c368e)
BewareMyPower
added a commit
that referenced
this pull request
Mar 24, 2022
### Motivation #14823 fixes the flaky `testConnectTimeout` but it's also a regression of #14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`. (cherry picked from commit 54c368e)
Nicklee007
pushed a commit
to Nicklee007/pulsar
that referenced
this pull request
Apr 20, 2022
Nicklee007
pushed a commit
to Nicklee007/pulsar
that referenced
this pull request
Apr 20, 2022
### Motivation apache#14823 fixes the flaky `testConnectTimeout` but it's also a regression of apache#14587. Because when the fd limit is reached, the `connectionTimeoutTask_` won't be initialized with a non-null value. Calling `stop` method on it directly will cause segmentation fault. See https://github.com/apache/pulsar/blob/0fe921f32cefe7648ca428cd9861f9163c69767d/pulsar-client-cpp/lib/ClientConnection.cc#L178-L185 ### Modifications Add the null check for `connectionTimeoutTask_` in `ClientConnection::close`.
BewareMyPower
added a commit
to BewareMyPower/pulsar-client-cpp
that referenced
this pull request
Feb 21, 2023
Fixes apache#202 ### Motivation When a `deadline_timer` is constructed, `boost::system::system_error` would be thrown if the fd limit is reached. In this case, the following APIs would fail with exception instead of returning a `Result`: - `createProducer` - `subscribe` - `createReader` P.S. This behavior is not documented in Boost's documents. Generally, we should not allow exceptions being thrown from those APIs because we return a `Result` to indicate the error. In addition, these exceptions cannot be caught by the wrapper of other languages. There is a similar issue: apache/pulsar#14582. However, apache/pulsar#14587 only fixes the case when creating a `socket`. The exception from the constructor of `deadline_timer` is not processed. ### Modifications Handle the exceptions when creating a socket or timer from `ExecutorService` to restart this executor, then propagate a `std::runtime_error`. Don't allow creating a timer directly from `io_service` so that exceptions can be handled well. In addition, catch the `runtime_error` when: - Creating a producer (`ProducerImpl` and `PartitionedProducerImpl`) - Creating a consumer (`ConsumerImpl` and `MultiTopicsConsumerImpl`) - Creating a reader (`ReaderImpl`) because the constructors of them all create at least one timer. ### Verifications Add `tests/unix/ConnectionFailTest.cc` for Unix systems because `setrlimit` is a POSIX API. It covers the cases when the fd limit is not enough to create a producer, a consumer and a reader.
Merged
1 of 4 tasks
BewareMyPower
added a commit
to BewareMyPower/pulsar-client-cpp
that referenced
this pull request
Feb 21, 2023
Fixes apache#202 ### Motivation When a `deadline_timer` is constructed, `boost::system::system_error` would be thrown if the fd limit is reached. In this case, the following APIs would fail with exception instead of returning a `Result`: - `createProducer` - `subscribe` - `createReader` P.S. This behavior is not documented in Boost's documents. Generally, we should not allow exceptions being thrown from those APIs because we return a `Result` to indicate the error. In addition, these exceptions cannot be caught by the wrapper of other languages. There is a similar issue: apache/pulsar#14582. However, apache/pulsar#14587 only fixes the case when creating a `socket`. The exception from the constructor of `deadline_timer` is not processed. ### Modifications Handle the exceptions when creating a socket or timer from `ExecutorService` to restart this executor, then propagate a `std::runtime_error`. Don't allow creating a timer directly from `io_service` so that exceptions can be handled well. In addition, catch the `runtime_error` when: - Creating a producer (`ProducerImpl` and `PartitionedProducerImpl`) - Creating a consumer (`ConsumerImpl` and `MultiTopicsConsumerImpl`) - Creating a reader (`ReaderImpl`) because the constructors of them all create at least one timer. ### Verifications Add `tests/unix/ConnectionFailTest.cc` for Unix systems because `setrlimit` is a POSIX API. It covers the cases when the fd limit is not enough to create a producer, a consumer and a reader.
BewareMyPower
added a commit
to BewareMyPower/pulsar-client-cpp
that referenced
this pull request
Feb 21, 2023
Fixes apache#202 ### Motivation When a `deadline_timer` is constructed, `boost::system::system_error` would be thrown if the fd limit is reached. In this case, the following APIs would fail with exception instead of returning a `Result`: - `createProducer` - `subscribe` - `createReader` P.S. This behavior is not documented in Boost's documents. Generally, we should not allow exceptions being thrown from those APIs because we return a `Result` to indicate the error. In addition, these exceptions cannot be caught by the wrapper of other languages. There is a similar issue: apache/pulsar#14582. However, apache/pulsar#14587 only fixes the case when creating a `socket`. The exception from the constructor of `deadline_timer` is not processed. ### Modifications Handle the exceptions when creating a socket or timer from `ExecutorService` to restart this executor, then propagate a `std::runtime_error`. Don't allow creating a timer directly from `io_service` so that exceptions can be handled well. In addition, catch the `runtime_error` when: - Creating a producer (`ProducerImpl` and `PartitionedProducerImpl`) - Creating a consumer (`ConsumerImpl` and `MultiTopicsConsumerImpl`) - Creating a reader (`ReaderImpl`) because the constructors of them all create at least one timer. ### Verifications Add `tests/unix/ConnectionFailTest.cc` for Unix systems because `setrlimit` is a POSIX API. It covers the cases when the fd limit is not enough to create a producer, a consumer and a reader.
shibd
pushed a commit
to apache/pulsar-client-cpp
that referenced
this pull request
Mar 2, 2023
…#203) * [fix] Handle exceptions when creating timers when fd limit is reached Fixes #202 ### Motivation When a `deadline_timer` is constructed, `boost::system::system_error` would be thrown if the fd limit is reached. In this case, the following APIs would fail with exception instead of returning a `Result`: - `createProducer` - `subscribe` - `createReader` P.S. This behavior is not documented in Boost's documents. Generally, we should not allow exceptions being thrown from those APIs because we return a `Result` to indicate the error. In addition, these exceptions cannot be caught by the wrapper of other languages. There is a similar issue: apache/pulsar#14582. However, apache/pulsar#14587 only fixes the case when creating a `socket`. The exception from the constructor of `deadline_timer` is not processed. ### Modifications Handle the exceptions when creating a socket or timer from `ExecutorService` to restart this executor, then propagate a `std::runtime_error`. Don't allow creating a timer directly from `io_service` so that exceptions can be handled well. In addition, catch the `runtime_error` when: - Creating a producer (`ProducerImpl` and `PartitionedProducerImpl`) - Creating a consumer (`ConsumerImpl` and `MultiTopicsConsumerImpl`) - Creating a reader (`ReaderImpl`) because the constructors of them all create at least one timer. ### Verifications Add `tests/unix/ConnectionFailTest.cc` for Unix systems because `setrlimit` is a POSIX API. It covers the cases when the fd limit is not enough to create a producer, a consumer and a reader. * Fix the thread safety issue * Remove unused change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fix #14582
Asio socket creation throws a C++ exception if it fails to create the native socket resource. We need to handle and close the connection.