-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Concurrency issue in typedthreads.nim #24591
Comments
Example with use of manual allocation: Results with MM ORC: SUMMARY: ThreadSanitizer: data race typedthreads.nim:154 in typedthreads::running(Thread<refnet::SocketImpl>) --mm:none: SUMMARY: ThreadSanitizer: data race typedthreads.nim:271 in typedthreads::createThread(var<Thread<refnet::SocketImpl>>, proc<refnet::SocketImpl>, refnet::SocketImpl) Nim Compiler Version 2.3.1 import
std/net,
std/locks,
std/os
type SocketOpts* = object
socket*: Socket
var lock: Lock = Lock()
proc processSession(sock: Socket) {.thread.} =
if sock.isNil:
withLock lock:
debugEcho "Client socket is nil"
return
withLock lock:
debugEcho "Thread ", getThreadId(), " got request to send to socket ", cast[uint16](sock[])
discard trySend(sock, "hello\n")
sleep(1000)
withLock lock:
sock.close
try:
initLock(lock)
except:
#log error
debugEcho "Unable to init rlock"
var
thr: array[0..10, Thread[Socket]]
sockets: array[0..10, Socket]
address: string = ""
client: Socket
for sock in sockets.mitems:
sock = cast[Socket](alloc(sizeof(SocketImpl)))
proc startServer*(srv: var SocketOpts): void =
try:
srv.socket = newSocket()
srv.socket.setSockOpt(OptReusePort, true)
srv.socket.setSockOpt(OptNoDelay, true, level = IPPROTO_TCP.cint)
srv.socket.bindAddr(Port(3333))
srv.socket.listen()
var id: int = 0
while true:
srv.socket.acceptAddr(client, address)
withLock lock:
if thr[id].running:
debugEcho "No threads available"
client.send("No threads available")
client.close()
continue
deepCopy(sockets[id], client)
echo "Client connected from: ", address, ". Socket: ", cast[uint16](sockets[id][])
createThread(thr[id], processSession, sockets[id])
client = Socket()
if id == thr.high:
id = 0
else:
id.inc
except:
#log error here!!!
debugEcho "ERROR!"
deinitLock lock
let excpt: ref Exception = getCurrentException()
debugEcho excpt.msg
return
deinitLock(lock)
var params: SocketOpts
startServer(params)
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
Thread sanitiser and valgrind (--tool=helgrind) report a data race (probably read) in typedthreads.nim on line 274 (Using devel version of the compiler. The problem also occurs with other compiler versions, but the reported line numbers vary.).
Example using channels:
Example of using an array to store client sockets:
nim.cfg :
Nim Version
Current devel version,
2.2.0
1.6.20
Current Output
Expected Output
No response
Known Workarounds
No response
Additional Information
To test the code, use the following tools in two separate terminals
watch -n 0.1 nc 127.0.0.1 3333
To test with valgrind (--tool=helgrind), the thread sanitiser should be disabled.
The text was updated successfully, but these errors were encountered: