Skip to content

Hang when shutting down blocking concurrent queue via interrupt #411

Description

@diehard2

When attempting to interrupt a blocking concurrent queue that is waiting on a time out, I'm experiencing a hang (until the timeout) in this line

        do {
#ifdef MOODYCAMEL_LIGHTWEIGHTSEMAPHORE_MONOTONIC
          rc = sem_clockwait(&m_sema, CLOCK_MONOTONIC, &ts);
#else
          rc = sem_timedwait(&m_sema, &ts);
#endif
        } while (rc == -1 && errno == EINTR);
        return rc == 0;

If I change it to respect the interrupt, the timeout doesn't work. However, I'm unable to ctrl-c in my program and shut it down cleanly. What can I do here short of implementing some sort of poll and a smaller timeout?

Also, out of curiosity, is there a benefit to using MOODYCAMEL_LIGHTWEIGHTSEMAPHORE_MONOTONIC?

Activity

  1. cameron314 commented on Mar 30, 2025

    @cameron314
    Owner

    That would be due to the while (errno == EINTR) loop. Signals for all sorts of (non-ctrl+c) reasons (including from I/O and debuggers) will interrupt the sem_timedwait, and this needs to be handled gracefully.

    Redesign your application to enqueue something on SIGINT in order to unblock the waiting thread, which can then exit gracefully.

    Yes, sem_clockwait is superior to sem_timedwait as it allows a monotonic clock to be used. This is only available in later versions of glibc. When compiling against such a version, MOODYCAMEL_LIGHTWEIGHTSEMAPHORE_MONOTONIC is automatically enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions