Skip to content

Remove need to call apteryx_init before making library calls and need to call apteryx_shutdown on program exit#290

Open
luukp wants to merge 5 commits into
alliedtelesis:masterfrom
luukp:no_init
Open

Remove need to call apteryx_init before making library calls and need to call apteryx_shutdown on program exit#290
luukp wants to merge 5 commits into
alliedtelesis:masterfrom
luukp:no_init

Conversation

@luukp
Copy link
Copy Markdown
Contributor

@luukp luukp commented Oct 11, 2017

No description provided.

This is preparatory to cleaning up correctly after a fork, and not
requiring apteryx_init to be called.

* Some unreachable code in apteryx_dump is removed. (prior ASSERT performs
  this check)
* apteryx_shutdown_force is changed to just keep calling apteryx_shutdown until
  it returns false, rather than relying on refcount. This means that technically
  it will be called once more than necessary.

Signed-off-by: Luuk Paulussen <luuk.paulussen@alliedtelesis.co.nz>
* Move the client init code done inside the lock to _apteryx_init_internal
* Split the code to start the callback server into a separate function and
  call from _apteryx_init_internal and from add_callback

Signed-off-by: Luuk Paulussen <luuk.paulussen@alliedtelesis.co.nz>
When a process linked to an initialized apteryx client library forks,
one of the threads and all of the internal data and socket handles are
copied to the new process.  All locks are left in their current state. This
means that in the child process, the state of the client is indeterminate.

Register pthread_atfork handlers to take the global lock and release it,
so that we at least have one lock we know is safe in the child process.
Also, set client data to NULL in the child process, so that it can no
longer interfere with any sockets, etc. created by the parent.  Currently,
if the child calls apteryx_shutdown, the parent process loses its callback
socket.

This breaks the test_double_fork unit test. That test expects to be able to
set/get after a fork without reinitialising.

Signed-off-by: Luuk Paulussen <luuk.paulussen@alliedtelesis.co.nz>
This will cause any RPC connections and Unix socket descriptors to be
cleaned up.  This will also be called when a forked process unloads the
library, but as we have set the client_data to NULL in
_apteryx_atfork_child, this will have no negative effect.

Signed-off-by: Luuk Paulussen <luuk.paulussen@alliedtelesis.co.nz>
Add a function that gets the apteryx client data if it exists and
initialises it if it does not.

Use this function in the apteryx client APIs.

The test_init function is removed as its assertions are no longer valid.

The test_double_fork test now passes again.

Signed-off-by: Luuk Paulussen <luuk.paulussen@alliedtelesis.co.nz>
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