Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document our de-facto thread-safety policy #2719

Closed
Zalathar opened this issue Dec 28, 2020 · 1 comment · Fixed by #2791
Closed

Document our de-facto thread-safety policy #2719

Zalathar opened this issue Dec 28, 2020 · 1 comment · Fixed by #2791
Labels
docs documentation could *always* be better opinions-sought tell us what you think about these ones!

Comments

@Zalathar
Copy link
Contributor

As pointed out in #2717, we don't appear to have any documented policy on whether or not Hypothesis is thread-safe.

Actually making Hypothesis more thread-safe would be a bunch of work and out-of-scope for this issue, so here I'm mostly focused on being more transparent about the current situation.

I see three main scenarios worth documenting:

  • Running in multiple processes (e.g. under pytest -nauto)

    • I don't know if we have explicit tests for this, but our own test suite routinely runs in multiple processes, so hopefully we should notice any problems pretty quickly.
    • This mostly involves making sure that our file-backed database can't tread on its own toes too badly.
  • Running separate tests in multiple threads

    • Hypothesis isn't explicitly designed or tested for this, so it's not formally “supported“, but anecdotally it does mostly work and we would like it to keep working.
    • This mostly involves being careful about global state, shared caches, and cached strategies.
    • We accept bug reports and reasonable patches.
  • Using multiple threads within a single test

    • Hypothesis assumes that tests are single-threaded, or do a sufficiently-good job of pretending to be single-threaded.
    • Tests that use helper threads internally should be OK, but the user must be careful to ensure that test outcomes are still deterministic.
      • Tests that change their dynamic draw behaviour based on helper-thread timing count as nondeterministic, so don't do that.
      • Deterministic stack traces might be a danger point here; I'm not sure.
    • Interacting with any Hypothesis APIs from helper threads might do weird/bad things, so don't do that.
      • This is because we rely on thread-local variables in a few places, and haven't explicitly tested/audited how they respond to cross-thread API calls.
      • Dynamic draws from st.data() are the most obvious danger, but other APIs might also be subtly affected.

Does this seem mostly accurate?

@Zalathar Zalathar added opinions-sought tell us what you think about these ones! docs documentation could *always* be better labels Dec 28, 2020
@Zac-HD
Copy link
Member

Zac-HD commented Dec 28, 2020

Yep, looks good to me - thanks for writing it up @Zalathar!

I'd note under "Running separate tests in multiple threads" that running a single test in multiple threads has all these problems but more so - it might work for simple examples, but I would expect this to be pretty fragile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs documentation could *always* be better opinions-sought tell us what you think about these ones!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants