Skip to content

[C++] Handle exception in creating socket when fd limit is reached - #14587

Merged
codelipenghui merged 2 commits into
apache:masterfrom
merlimat:fix-fd-error
Mar 9, 2022
Merged

codelipenghui merged 2 commits into
apache:masterfrom
merlimat:fix-fd-error

Conversation

@merlimat

@merlimat merlimat commented Mar 7, 2022

Copy link
Copy Markdown
Contributor

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.

@merlimat merlimat added type/bug The PR fixed a bug or issue reported a bug component/c++ release/2.10.1 labels Mar 7, 2022
@merlimat merlimat added this to the 2.11.0 milestone Mar 7, 2022
@merlimat merlimat self-assigned this Mar 7, 2022
@github-actions

github-actions Bot commented Mar 7, 2022

Copy link
Copy Markdown

@merlimat:Thanks for your contribution. For this PR, do we need to update docs?
(The PR template contains info about doc, which helps others know more about the changes. Can you provide doc-related info in this and future PR descriptions? Thanks)

@merlimat merlimat added doc-not-needed Your PR changes do not impact docs and removed doc-label-missing labels Mar 7, 2022

@BewareMyPower BewareMyPower left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we catch the exception in ConnectionPool:: getConnectionAsync instead of inside the constructor?

ClientConnectionPtr cnx(new ClientConnection(logicalAddress, physicalAddress, executorProvider_->get(),
clientConfiguration_, authentication_));

Because if the socket object failed to construct, the ClientConnection object will still be inserted into the connection pool.

@codelipenghui
codelipenghui merged commit babae8e into apache:master Mar 9, 2022
@merlimat
merlimat deleted the fix-fd-error branch March 10, 2022 16:39
codelipenghui pushed a commit that referenced this pull request Mar 12, 2022
@codelipenghui codelipenghui modified the milestones: 2.11.0, 2.10.0 Mar 12, 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 pushed a commit that referenced this pull request Mar 24, 2022
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 BewareMyPower added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label Mar 24, 2022
BewareMyPower pushed a commit that referenced this pull request Mar 24, 2022
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 BewareMyPower added the cherry-picked/branch-2.8 Archived: 2.8 is end of life label Mar 24, 2022
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.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-picked/branch-2.8 Archived: 2.8 is end of life cherry-picked/branch-2.9 Archived: 2.9 is end of life doc-not-needed Your PR changes do not impact docs release/2.8.4 release/2.9.3 type/bug The PR fixed a bug or issue reported a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++/Python pulsar client abort()s or segfaults on file descriptor exhaustion ("Too many open files")

3 participants