Segfault when DHCP server using CB terminates (clang only)
When running some manual tests of the CB on macOS I noticed that it causes a segfault upon termination. This doesn't impact the operation of the DHCP server. It occurs when the process exits and destructor of the
CfgMgr is invoked. The snippet https://gitlab.isc.org/snippets/840 includes a back trace. This issue only occurs when Kea is compiled with clang.
The direct cause of this issue is that we allocate objects such as subnets, shared networks etc. within the heap that belongs to the dynamically loaded library (hooks library) and then pass them back to the binary (Kea server). As long as the library is loaded the Kea server can safely use those objects. However, when the library is unloaded the memory space dedicated for the library is gone and no longer accessible by the binary. As a result, the binary segfaults when trying to destroy the object.
One of the considered solutions was to make the library clear the configuration when it is being unloaded. I tried that and it indeed prevented the crash. However, we want the configuration to outlive the library. Especially that there is no easy way to clear the configuration that was introduced by the library and preserve all the rest.
The better solution which I tested was to create the static factory functions within Kea, e.g.
Subnet4::create which would return the shared pointer to the newly created object. The library must call this factory function (rather than create the instance on its own) which will cause the instance to be created by the binary, rather than the library. That way, when the library is unloaded the object is still available.