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

Fixes for typedthreads data race issues #24612

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from
Open

Conversation

mk1nz
Copy link

@mk1nz mk1nz commented Jan 11, 2025

fixes #24591

Modifications have been tested with examples in the open issue 24591. All seem to work except one with Thread[Socket] because of ref SocketImpl. Needs testing in Linux and Windows, probably better with some existing software that uses typedthreads to make sure I have not broken it.

Modifications have been tested with examples in the open issue. All seem to work except one with Thread[Socket] because of ref SocketImpl. Needs testing in Linux and Windows, probably better with some existing software that uses typedthreads to make sure I have not broken it.
@mk1nz
Copy link
Author

mk1nz commented Jan 11, 2025

fixes #24591

@mk1nz
Copy link
Author

mk1nz commented Jan 13, 2025

cpp targets fail to compile, probably needs this: https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper

when TArg isnot void: t.data = param
t.dataFn = tp
when TArg isnot void: t.data.store param
t.dataFn.store tp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these loads/stores can use relaxed memory ordering

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set moAcquireRelease, and if I'm right, this will work just like withLock

@@ -149,9 +149,9 @@ else:
nimThreadProcWrapperBody(closure)
{.pop.}

proc running*[TArg](t: Thread[TArg]): bool {.inline.} =
proc running*[TArg](t: var Thread[TArg]): bool {.inline.} =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the var here is a breaking change, why is it needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please ignore. If I manage to fix other issues that arise, this change will be removed with the next commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the var here is a breaking change, why is it needed?

nim c --styleCheck:usages --styleCheck:error --verbosity:0 --hints:off --skipParentCfg --skipUserCfg --outdir:build '--nimcache:build/nimcache/$projectName' -d:metricsTest -d:metrics --threads:on -d:nimTypeNames --mm:refc -r tests/chronos_server_tests
/Users/runner/work/Nim/Nim/pkgstemp/metrics/metrics/chronos_httpserver.nim(400, 6) template/generic instantiation of async from here
/Users/runner/work/Nim/Nim/pkgstemp/metrics/metrics/chronos_httpserver.nim(403, 26) template/generic instantiation of running from here
/Users/runner/work/Nim/Nim/lib/std/typedthreads.nim(155, 22) Error: type mismatch
Expression: load(t.dataFn, moAcquireRelease)
[1] t.dataFn: Atomic[proc (m: MetricsServerData){.nimcall, gcsafe.}]
[2] moAcquireRelease: MemoryOrder

Expected one of (first mismatch at [position]):
[1] proc load[T: Trivial](location: var Atomic[T];
order: MemoryOrder = moSequentiallyConsistent): T
[1] proc load[T: not Trivial](location: var Atomic[T];
order: MemoryOrder = moSequentiallyConsistent): T
expression 't.dataFn' is immutable, not 'var'

mk1nz added 3 commits January 18, 2025 20:12
For the cpp target, the old flow without atomics is retained.
With --tlsEmulation:on enabled, GC calls GetThreadLocalVars() before it calls initThreadVarsEmulation from threadlocalstorage.nim, causing a segfault. This should be fixed to make this PR pass tests.
@mk1nz
Copy link
Author

mk1nz commented Jan 18, 2025

For the cpp target, the old flow without atomics is retained.
With --tlsEmulation:on enabled, GC calls GetThreadLocalVars() before it calls initThreadVarsEmulation from threadlocalstorage.nim, causing a segfault. This should be fixed to make this PR pass tests.

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.

Concurrency issue in typedthreads.nim
2 participants