Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Remove closed producers and consumers from client #2

Closed
wants to merge 1 commit into from

Conversation

BewareMyPower
Copy link
Owner

No description provided.

@BewareMyPower BewareMyPower force-pushed the bewaremypower/fix-close-handle branch from e22d206 to cc83de2 Compare October 19, 2022 08:46
Fixes apache#55

### Motivation

1. When a producer or consumer is closed, the reference is still stored
   in `ClientImpl`. If a client kept creating producers or consumers,
   the memory usage would not reduce.
2. When the `HandlerBase::connection_` field is modified, the
   `removeProducer` or `removeConsumer` method is not called. Then these
   producers and consumers will be cached in the connection until the
   connection is closed.
3. The `PartitionedProducerImpl` and `MultiTopicsConsumerImpl` have
   cyclic references, when a `Producer` or `Consumer` instance goes out
   of the scope, the destructors are not called. When I used GDB to
   debug them, I found the reference counts were both greater than 1.

### Modifications

Let's use "handlers" to represent "producers and consumers".

1. In `ClientImpl`, use `SynchronizedHashMap` to store references of
   handlers, as well as the `cleanupXXX` methods to remove a handler.
2. Add `HandlerBase::beforeConnectionChange` method, which is called
   before `connection_` is modified. Disallow the access to
  `connection_` from derived classes.
3. Avoid `shared_from_this()` being passed into callbacks in ASIO
   executors for `PartitionedProducerImpl` and
   `MultiTopicsConsumerImpl`.

This PR also unifies the `shutdown` implementations for handlers and
call `shutdown` in the destructors.
1. Cancel the timers
2. Unregister itself from `ClientImpl` and `ClientConnection`
3. Set the create future with `ResultAlreadyClosed`
4. Set the state to `Closed`

It's called when:
- the destructor is called
- `closeAsync` is completed
- `unsubscribeAsync` is completed with ResultOk

### Verifications

`ShutdownTest` is added to verify the following cases:
- a single topic
- a partitioned topic (multiple topics)
- a partitioned topic with regex subscription

`testClose` verifies `shutdown` when `closeAsync` and `unsubscribeAsync`
are called. `testDestructor` verifies `shutdown` when handlers go out of
the scope and the destructors are called.
@BewareMyPower BewareMyPower force-pushed the bewaremypower/fix-close-handle branch from cc83de2 to 45fe40f Compare October 20, 2022 10:14
@BewareMyPower BewareMyPower deleted the bewaremypower/fix-close-handle branch November 8, 2022 08:45
BewareMyPower added a commit that referenced this pull request Sep 11, 2023
### Motivation

When libcurl is used in `AuthOauth2`, the `CURLOPT_NOSIGNAL` option is
not set, i.e. it will be the default value so that the
`Curl_resolv_timeout` function might crash in multi-threading
environment.

```
#2 0xf630 in _L_unlock_13 from /lib64/libpthread.so.0 (0x34)
#3 0x2e6c7f in Curl_failf from /usr/local/bin/***/libpulsar.so (0x6f)
#4 0x30a285 in Curl_resolv_timeout from /usr/local/bin/***/libpulsar.so (0x95)
```

Since there are many duplicated code when calling curl C APIs, it's hard to
notice that `CURLOPT_NOSIGNAL` is not configured in `AuthOauth2`.

### Modifications

Introduce a `CurlWrapper` class that sets the same options to reduce the
duplicated code and adapting consistent behaviors unless a few options.
BewareMyPower added a commit that referenced this pull request Sep 13, 2023
…#313)

### Motivation

When libcurl is used in `AuthOauth2`, the `CURLOPT_NOSIGNAL` option is
not set, i.e. it will be the default value so that the
`Curl_resolv_timeout` function might crash in multi-threading
environment.

```
#2 0xf630 in _L_unlock_13 from /lib64/libpthread.so.0 (0x34)
#3 0x2e6c7f in Curl_failf from /usr/local/bin/***/libpulsar.so (0x6f)
#4 0x30a285 in Curl_resolv_timeout from /usr/local/bin/***/libpulsar.so (0x95)
```

Since there are many duplicated code when calling curl C APIs, it's hard to
notice that `CURLOPT_NOSIGNAL` is not configured in `AuthOauth2`.

### Modifications

Introduce a `CurlWrapper` class that sets the same options to reduce the
duplicated code and adapting consistent behaviors unless a few options.
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.

1 participant