Skip to content

[C++] Fix segmentation fault when creating socket failed - #14834

Merged
BewareMyPower merged 1 commit into
apache:masterfrom
BewareMyPower:bewaremypower/cpp-socket-npe
Mar 24, 2022
Merged

BewareMyPower merged 1 commit into
apache:masterfrom
BewareMyPower:bewaremypower/cpp-socket-npe

Conversation

@BewareMyPower

Copy link
Copy Markdown
Contributor

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

try {
socket_ = executor_->createSocket();
connectTimeoutTask_ = std::make_shared<PeriodicTask>(executor_->getIOService(),
clientConfiguration.getConnectionTimeout());
consumerStatsRequestTimer_ = executor_->createDeadlineTimer();
} catch (const boost::system::system_error& e) {
LOG_ERROR("Failed to initialize connection: " << e.what());
close();

Modifications

Add the null check for connectionTimeoutTask_ in ClientConnection::close.

### 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 BewareMyPower added type/bug The PR fixed a bug or issue reported a bug component/client-c++ doc-not-needed Your PR changes do not impact docs release/2.10.1 labels Mar 24, 2022
@BewareMyPower BewareMyPower added this to the 2.11.0 milestone Mar 24, 2022
@BewareMyPower BewareMyPower self-assigned this Mar 24, 2022
@BewareMyPower
BewareMyPower marked this pull request as draft March 24, 2022 03:32
@BewareMyPower
BewareMyPower marked this pull request as ready for review March 24, 2022 03:58
@BewareMyPower

Copy link
Copy Markdown
Contributor Author

It looks hard to add a test. I tried to set the resource limit in tests but cannot restore the original value.

#ifndef _MSC_VER
TEST(ClientTest, testTooManyFiles) {
    struct rlimit originalLimit;
    getrlimit(RLIMIT_NOFILE, &originalLimit);

    struct rlimit currentLimit;
    currentLimit.rlim_cur = 1;
    currentLimit.rlim_max = 1;
    setrlimit(RLIMIT_NOFILE, &currentLimit);

    /* Tests here ... */

    setrlimit(RLIMIT_NOFILE, &originalLimit);  // it failed with Operation not permitted
}
#endif

@michaeljmarshall michaeljmarshall 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.

Thanks for your helpful explanation @BewareMyPower! LGTM

@BewareMyPower
BewareMyPower merged commit 54c368e into apache:master Mar 24, 2022
@BewareMyPower
BewareMyPower deleted the bewaremypower/cpp-socket-npe branch March 24, 2022 07:29
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 BewareMyPower added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label 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
### 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`.
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 cherry-picked/branch-2.10 doc-not-needed Your PR changes do not impact docs release/2.8.4 release/2.9.3 release/2.10.1 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.

3 participants