Currently ThreadContext Connection* pointers are not removed up when a connection is destroyed. This is only a problem if a Connection instance is destroyed and new Connection is allocated at the same address, because the code assumes pointers uniquely identify connections. This causes a bug in a bitcoin IPC test which creates multiple connections in a loop, described in https://github.com/bitcoin/bitcoin/pull/30509#issuecomment-2276739868, and depending on how the heap allocator behaves, a new Connection could have the same address as a previously destroyed connection, and the code tries to use a thread reference associated with the previous connection when making a new call, and there is a segfault because the thread no longer exists.
Fix this problem by adding Connection cleanup callbacks to remove Connection* pointers from the ThreadContext struct if the connection is destroyed before the thread is.
Currently ThreadContext Connection* pointers are not removed up when a connection is destroyed. This is only a problem if a Connection instance is destroyed and new Connection is allocated at the same address, because the code assumes pointers uniquely identify connections. This causes a bug in a bitcoin IPC test which creates multiple connections in a loop, described in https://github.com/bitcoin/bitcoin/pull/30509#issuecomment-2276739868, and depending on how the heap allocator behaves, a new Connection could have the same address as a previously destroyed connection, and the code tries to use a thread reference associated with the previous connection when making a new call, and there is a segfault because the thread no longer exists.
Fix this problem by adding Connection cleanup callbacks to remove Connection* pointers from the ThreadContext struct if the connection is destroyed before the thread is.