Add a way to reset GSDK in cppsdk - #178
Conversation
Adds a way to stop gsdk before calling exit() or exiting main
|
hey artkuy thanks for the contribution. This looks like a change in the public GSDK API, which makes it hard to accept since we'll have to change all other implementations as well. Can you shed more light on the issue your PR tries to solve? Maybe there's something else we can do on the C++ side to prevent this issue from happening. |
|
Hi Dimitris Gkanatsios (@dgkanatsios), in our use case it's a problem that We need a way to control the lifetime of GSDK and stop it before we destroy our memory allocator. |
|
thank you for the explanation - I have asked C++ experts in our team to take a look at it. |
|
Perhaps, but this seems rather prone to future issues as it's any dynamic allocation that would cause crashing. So any future addition to the project could quickly break it again. |
|
artkuy please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
Adds explicit GSDK lifecycle teardown to stop background work before application shutdown.
Changes:
- Adds the public
GSDK::reset()API. - Stops and joins the heartbeat thread before resetting internal state.
- Guards thread joining with
joinable().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
cpp/cppsdk/gsdk.h |
Declares and documents reset(). |
cpp/cppsdk/gsdk.cpp |
Implements heartbeat shutdown and instance reset. |
Suppressed comments (2)
cpp/cppsdk/gsdk.cpp:522
reset()can block for the full agent-provided heartbeat interval (which is not capped) because the heartbeat thread may be waiting onm_signalHeartbeatEvent. When that wait eventually expires, the loop also sends one more heartbeat before observing the false flag. Signal the event during reset and re-checkm_keepHeartbeatRunningimmediately afterWait()so teardown wakes promptly without issuing another request.
GSDKInternal::get().m_keepHeartbeatRunning = false;
if (GSDKInternal::get().m_heartbeatThread.joinable())
{
GSDKInternal::get().m_heartbeatThread.join();
cpp/cppsdk/gsdk.cpp:525
- A shutdown callback is executed by
m_shutdownThread; if that callback calls this new teardown API before exiting, resetting the instance destroys thestd::futurefor the currently executing async task. The future destructor waits for that task to finish, so the callback deadlocks waiting on itself. Keep callback-task ownership outsideGSDKInternal, or defer instance destruction when reset is invoked from the callback thread.
GSDKInternal::m_instance.reset();
| GSDKInternal::get(); | ||
| } | ||
|
|
||
| void GSDK::reset() |
| if (m_heartbeatThread.joinable()) | ||
| { | ||
| m_heartbeatThread.join(); | ||
| } |
This PR modifies the cppsdk to add a way to stop GSDK before calling exit() or exiting main.
Allows user to control lifetime of GSDK, needed to prevent allocations from happening after custom memory allocator has been destroyed. This is done by GSDK::logMessage from the heartbeat thread.