Skip to content

IOSimPOR: racyByLabel - #274

Open
coot wants to merge 3 commits into
mainfrom
coot/racy-by-label
Open

coot wants to merge 3 commits into
mainfrom
coot/racy-by-label

Conversation

@coot

@coot coot commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds a more control over which threads are part of schedule exploration. It relies on two API changes:

  • explorationRaceByLabel callback field for ExplorationOptions (with two helpers withRacyMainThread and withRaceByLabel
  • new forkLabelledIO, asyncLabelled, withAsyncLabelled & friends, which must be used so the label is available when forked thread is created rather than altered when the forked read is being executed.

An alternative design where IOSimThreadId is no longer a type with two constructors RacyThreadId and ThreadId, which wouldn't require speciall forkLabelledIO API is possible, but it is significant change to the IOSimPOR API, and thus I decided to not pursue it. The advantage of the implemented design is simplicity of IOSimPOR changes (Fork carries ThreadLabel field), all other changes are peripheral to IOSimPOR race discovery logic, plus changes to MonadFork and MonadAsync.

@coot
coot requested a review from a team as a code owner November 13, 2026 16:14
@coot
coot force-pushed the coot/racy-by-label branch from 1f42655 to 1c9bc01 Compare November 13, 2026 16:19
coot added 3 commits November 14, 2026 11:13
The default implementation of `withAsyncWithUnmask` (used by IOSim, but
not IO), didn't restore masking state the same way `async` and `base`
packages do.  This only matters if the `withAsyncWithUnmask` is
called when exception are `Unmasked`.   The correct implementation
(below) uses `restore` in
```hs
 withAsyncWithUnmask action inner = mask $ \restore -> do
                                      a <- asyncWithUnmask (\unmask -> restore (action unmask))
                                      restore (inner a)
                                        `finally` uninterruptibleCancel a
```
to restores the original `Unmasked` state.

The buggy implementation:
```hs
 withAsyncWithUnmask action inner = mask $ \restore -> do
                                      a <- asyncWithUnmask action
                                      restore (inner a)
                                        `finally` uninterruptibleCancel a
```
when called with exceptions `Unmasked`, will run `action` in
`InterruptibleMasked` state instead.

There's not difference between the two implementations when the function
is called when exceptions are masked or uninterruptibly masked.

The commit includes tests for `IO` and `IOSim` instances.
IOSimPOR can now select the threads which take par ti in race discovery
by their label with the following rules:

* threads must be started by one of `forkLabelledIO` or `asyncLabelled`,
  `withAsyncLabelled` and friends - this makes sure the thread label is
  available in `IOSim` when a thread is created rather than set by
  `labelThisThread` when it is scheduled
* racy flag is sticky, e.g. threads started by a thread that are part of
  race discovery are part of the race discovery.

You need to set the new `explorationRacyByLabel` field of
`ExplorationOptions`, either directly or by using one of the two
combinators:
* `withRacyMainThread` - IOSimPOR will make all threads part of schedule
  exploration (this wasn't possible before)
* `withRacyByLabel` - by passing a callback the simulation can select
  which threads will be part race exploration

The test-suite is using now internal API, and thus depends on `io-sim`
sources directly.  This way we can keep internal API unexposed to the
user.  Almost all `IOSimPOR` tests are using now `withRacyMainThread`
- for that reason we had to drop `withNumTests 1000` from two tests.

New tests are included for labelled functions.
@coot
coot force-pushed the coot/racy-by-label branch from 1c9bc01 to 553d462 Compare November 14, 2026 21:13
@coot coot added IOSimPOR Issues / PRs related to IOSimPOR leios Issues, PRs related to leios labels Nov 14, 2026
@coot coot self-assigned this Nov 14, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in Ouroboros Network Nov 14, 2026

This branch has not been deployed

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

Labels

IOSimPOR Issues / PRs related to IOSimPOR leios Issues, PRs related to leios

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant