Skip to content

Data race on ctx->threadLimit between POOL_thread() and POOL_create_advanced() #4547

Closed

Description

@guoyuqi020

It seems that the ctx->threadLimit variable is updated in POOL_create_advanced() without being protected by the ctx->queueMutex. This can lead to a data race. Will this be a problem?

https://github.com/facebook/zstd/blob/bfd8ad886f2948a8a0159a28e0aa6153611cd1c1/lib/common/pool.c#L67-L90

https://github.com/facebook/zstd/blob/bfd8ad886f2948a8a0159a28e0aa6153611cd1c1/lib/common/pool.c#L143-L159

Activity

  1. sebres commented on Mar 1, 2026

    @sebres
    Contributor

    There is basically no RC at the moment, since the ctx (created here) isn't used somewhere else yet, excepting the workers in pool, where the first of them would hold the lock in:
    https://github.com/facebook/zstd/blob/ae9f20ca2716f2605822ca375995b7d876389b64/lib/common/pool.c#L72
    and then enter the conditional wait, because of ctx->queueEmpty, no matter what ctx->threadLimit is, because by || (or) the value of (ctx->numThreadsBusy >= ctx->threadLimit) is irrelevant:
    https://github.com/facebook/zstd/blob/ae9f20ca2716f2605822ca375995b7d876389b64/lib/common/pool.c#L74-L75

    But to avoid possible RC in the future (if the logic would be not enough anymore) one could indeed rewrite it a bit.
    The naive attempt to "fix" it with lock around set of #4558 is in my opinion not correct (even wise versa, can cause a sporadic deadlock).
    I'll provide a new PR for that.

  2. added a commit that references this issue on Mar 1, 2026
    3587c93
  3. added a commit that references this issue on Mar 2, 2026
    6e1e545
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions