Skip to content

Issue about sentry__enter_signal_handler #1353

@craftoq

Description

@craftoq

Description
The implementation of sentry__enter_signal_handler in sentry_sync.c has potential issues related to atomicity and race conditions.

The function is implemented as:

void sentry__enter_signal_handler(void)
{
    sentry__block_for_signal_handler();
    g_signal_handling_thread = sentry__current_thread();
    __sync_fetch_and_or(&g_in_signal_handler, 1);
}

Issues:

  1. Non-atomic entry:
    The check-and-set for entering the signal handler is not atomic. After sentry__block_for_signal_handler() returns, another thread may also reach this line and set the flags, causing multiple threads to believe they are in the handler.

  2. Return value ignored:
    The return value of sentry__block_for_signal_handler() is not checked. If the current thread is already the signal-handling thread, the function should probably return early, but it continues, possibly overwriting g_signal_handling_thread and setting the flag again.

  3. Split flag and thread ID:
    Setting g_signal_handling_thread and g_in_signal_handler happens in two separate steps, which can lead to inconsistent states in a multithreaded or signal-driven environment.

Metadata

Metadata

Assignees

Labels

BugSomething isn't workingNative

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions