Skip to content

Support forking processes for KeysInUse#146

Merged
mamckee merged 5 commits intoscossl-1.9from
mamckee-keysinuse-child-proc
Sep 19, 2025
Merged

Support forking processes for KeysInUse#146
mamckee merged 5 commits intoscossl-1.9from
mamckee-keysinuse-child-proc

Conversation

@mamckee
Copy link
Collaborator

@mamckee mamckee commented Sep 17, 2025

KeysInUse relies on the logging thread to periodically log events. The logging thread is created when the SymCrypt provider is initialized, and runs for the lifetime of the provider. If the process forks, only the main thread and all global data are copied. Any additional threads, including the main thread, are not cloned in the child process. This means that only the parent process will emit KeysInUse events, and child processes will not.

This PR adds a new function to keysinuse p_scossl_keysinuse_atfork_reinit. This function is registered to run in the child process after fork with pthread_atfork. The function will recreate the logging thread and reinitialize global state in the child process to enable continued keysinuse logging.

This change was verified with a test application that creates a private key, forks child processes, and uses the cloned key object in the child processes to ensure keys loaded in the parent process continue to emit keysinuse telemetry in child processes. I also verified this with nginx and confirmed keysinuse events were correctly written after the change.

  • Added p_scossl_keysinuse_atfork_reinit callback to recreate the logging thread in the child process after fork
  • Moved the sk_keysinuse_info_pending pointer to be global instead of local to the logging thread
    • The underlying stack object is allocated on the heap and therefore gets cloned after fork. It needs to be properly cleaned up in the child process to avoid duplicate events and prevent a memory leak.

Copy link
Contributor

@samuel-lee-msft samuel-lee-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo comments

Copy link
Contributor

@samuel-lee-msft samuel-lee-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mamckee mamckee merged commit 1c2f0f0 into scossl-1.9 Sep 19, 2025
3 checks passed
mamckee added a commit that referenced this pull request Sep 29, 2025
mamckee added a commit that referenced this pull request Sep 29, 2025
@mamckee mamckee deleted the mamckee-keysinuse-child-proc branch October 9, 2025 19:03
mamckee added a commit that referenced this pull request Jan 22, 2026
* Recreate logging thread at fork

* Clean pending events in log thread reinit

* Only recreate logging thread if it was running in the parent process

* Modify keysinuse stack and infos under lock in callback

* Move mutex reinitialization
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.

2 participants