Skip to content

[MrBot] Add robust_semaphore, a cross-process semaphore that survives holder crashes - #655

Open
Dan Cristoloveanu (dcristoloveanu) wants to merge 2 commits into
masterfrom
dcristo/robust_semaphore
Open

Dan Cristoloveanu (dcristoloveanu) wants to merge 2 commits into
masterfrom
dcristo/robust_semaphore

Conversation

@dcristoloveanu

Copy link
Copy Markdown
Member

Adds robust_semaphore (win32), a named cross-process counting semaphore whose units are given back when the holding process dies.

A Windows semaphore is not owned by anyone, so a unit taken by a process that crashes is lost until the semaphore is recreated. robust_semaphore uses N named mutexes <name>_<i> instead: a mutex is owned by a thread and is released (abandoned) by the OS when that thread or process dies.

API

  • robust_semaphore_create(name, max_count, security_descriptor): opens the N mutexes (CreateMutexExA, optional SDDL) and starts one worker thread for the object. max_count is 1..63.
  • robust_semaphore_acquire_async(h, timeout_ms, max_hold_ms, on_acquire_complete, context): one acquisition at a time per object. The callback always runs on the worker thread (never inline) with OK + unit index, TIMEOUT, CANCELLED or ERROR.
  • robust_semaphore_cancel(h): stops a waiting acquisition.
  • robust_semaphore_release(h): ends the hold (or stops a waiting acquisition).
  • robust_semaphore_destroy(h): releases, stops and joins the worker. It can be called from on_acquire_complete, in which case the worker frees the object.
  • max_hold_ms is counted from acquisition and includes the callback, so a unit is never held longer than that.

Tests

  • 45 unit tests (VLD clean).
  • 14 integration tests:
    • at most max_count holders; separate names don't share units;
    • SDDL; cancel; timeout; max hold, including an overrunning callback;
    • abandoned mutex, and a thread that exits while holding a unit;
    • a holder process (robust_semaphore_tester) that is killed;
    • destroy while waiting, while holding, and from the callback.
  • Design and requirements: win32/devdoc/robust_semaphore_requirements.md.

… whose units are released when the holding process dies
@dcristoloveanu

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@dcristoloveanu

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

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