Skip to content

Commit fdbcbec

Browse files
committed
Ensure channels don't leak exception effects
The forward declarations cause `Exception` to be inferred - also, `llrecv` is an internal implementation detail and the type of the received item is controlled by generics, thus the ValueError raised there seems out of place for the generic api.
1 parent 6656084 commit fdbcbec

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/system/channels_builtin.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
## localChannelExample() # "Hello from the main thread!"
139139
## ```
140140

141+
{.push raises: [], gcsafe.}
142+
141143
when not declared(ThisIsSystem):
142144
{.error: "You must not import this module explicitly".}
143145

@@ -390,7 +392,7 @@ proc llRecv(q: PRawChannel, res: pointer, typ: PNimType) =
390392
q.ready = false
391393
if typ != q.elemType:
392394
releaseSys(q.lock)
393-
raise newException(ValueError, "cannot receive message of wrong type")
395+
raiseAssert "cannot receive message of wrong type"
394396
rawRecv(q, res, typ)
395397
if q.maxItems > 0 and q.count == q.maxItems - 1:
396398
# Parent thread is awaiting in send. Wake it up.
@@ -455,3 +457,5 @@ proc ready*[TMsg](c: var Channel[TMsg]): bool =
455457
## new messages.
456458
var q = cast[PRawChannel](addr(c))
457459
result = q.ready
460+
461+
{.pop.}

tests/threads/tmembug.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var
1212
chan1.open()
1313
chan2.open()
1414

15-
proc routeMessage*(msg: BackendMessage) =
15+
proc routeMessage*(msg: BackendMessage) {.raises: [], gcsafe.} = # no exceptions!
1616
discard chan2.trySend(msg)
1717

1818
var
1919
recv: Thread[void]
2020
stopToken: Atomic[bool]
2121

22-
proc recvMsg() =
22+
proc recvMsg() {.raises: [], gcsafe.} = # no exceptions!
2323
while not stopToken.load(moRelaxed):
2424
let resp = chan1.tryRecv()
2525
if resp.dataAvailable:

0 commit comments

Comments
 (0)