diff --git a/tests/extensions/base64ext.nim b/tests/extensions/base64ext.nim index 0ffd24fca0..fc03d1c9fa 100644 --- a/tests/extensions/base64ext.nim +++ b/tests/extensions/base64ext.nim @@ -95,7 +95,7 @@ proc base64Factory*(padding: bool): ExtFactory = proc factory(isServer: bool, args: seq[ExtParam]): Result[Ext, string] {. - gcsafe, raises: [Defect].} = + gcsafe, raises: [].} = # you can capture configuration variables via closure # if you want diff --git a/tests/extensions/hexext.nim b/tests/extensions/hexext.nim index 9da8f37ca3..9be5bb3d75 100644 --- a/tests/extensions/hexext.nim +++ b/tests/extensions/hexext.nim @@ -83,7 +83,7 @@ proc hexFactory*(): ExtFactory = proc factory(isServer: bool, args: seq[ExtParam]): Result[Ext, string] {. - gcsafe, raises: [Defect].} = + gcsafe, raises: [].} = # you can capture configuration variables via closure # if you want diff --git a/tests/helpers.nim b/tests/helpers.nim index 78d1ece8c8..77484bc723 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -19,13 +19,15 @@ import pkg/[ import ../websock/websock import ./keys +{.push gcsafe, raises: [].} + const WSPath* = when defined secure: "/wss" else: "/ws" -proc rndStr*(size: int): string = +proc rndStr*(size: int): string {.gcsafe, raises: [].} = for _ in 0.. 0: hostName else: host.split(":")[0] + template used(x: typed) = + # silence unused warning + discard + let addrs = resolveTAddress(host) for a in addrs: try: @@ -190,6 +194,7 @@ proc connect*( return conn except TransportError as exc: + used(exc) trace "Error connecting to address", address = $a, exc = exc.msg raise newException(HttpError, diff --git a/websock/http/common.nim b/websock/http/common.nim index fbfcc992e7..ba21332174 100644 --- a/websock/http/common.nim +++ b/websock/http/common.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/[uri] import pkg/[ diff --git a/websock/http/server.nim b/websock/http/server.nim index e4f7a63a09..1eee8edc93 100644 --- a/websock/http/server.nim +++ b/websock/http/server.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/uri import pkg/[ @@ -43,6 +43,10 @@ type TlsHttpServer* = HttpServer +template used(x: typed) = + # silence unused warning + discard + proc validateRequest( stream: AsyncStreamWriter, header: HttpRequestHeader): Future[ReqStatus] {.async.} = @@ -69,7 +73,7 @@ proc parseRequest( ## var buffer = newSeq[byte](MaxHttpHeadersSize) - let remoteAddr = stream.reader.tsource.remoteAddress() + let remoteAddr {.used.} = stream.reader.tsource.remoteAddress() trace "Received connection", address = $remoteAddr try: let hlenfut = stream.reader.readUntil( @@ -115,11 +119,12 @@ proc parseRequest( # remote peer disconnected trace "Remote peer disconnected", address = $remoteAddr except TransportOsError as exc: + used(exc) trace "Problems with networking", address = $remoteAddr, error = exc.msg proc handleConnCb( server: StreamServer, - transp: StreamTransport) {.async.} = + transp: StreamTransport) {.async: (raises: []).} = var stream: AsyncStream try: stream = AsyncStream( @@ -131,10 +136,15 @@ proc handleConnCb( await httpServer.handler(request) except CatchableError as exc: + used(exc) debug "Exception in HttpHandler", exc = exc.msg finally: - await stream.closeWait() - + try: + await stream.closeWait() + except CatchableError as exc: + used(exc) + debug "Exception in HttpHandler closewait", exc = exc.msg + proc handleTlsConnCb( server: StreamServer, transp: StreamTransport) {.async.} = @@ -158,6 +168,7 @@ proc handleTlsConnCb( await httpServer.handler(request) except CatchableError as exc: + used(exc) debug "Exception in HttpHandler", exc = exc.msg finally: await stream.closeWait() @@ -208,7 +219,7 @@ proc create*( headersTimeout = HttpHeadersTimeout, handshakeTimeout = 0.seconds ): HttpServer - {.raises: [Defect, CatchableError].} = # TODO: remove CatchableError + {.raises: [CatchableError].} = # TODO: remove CatchableError ## Make a new HTTP Server ## diff --git a/websock/session.nim b/websock/session.nim index 30d82f974e..e93381248e 100644 --- a/websock/session.nim +++ b/websock/session.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/strformat import pkg/[chronos, chronicles, stew/byteutils, stew/endians2] @@ -18,6 +18,10 @@ import pkg/chronos/streams/asyncstream logScope: topics = "websock ws-session" +template used(x: typed) = + # silence unused warning + discard + proc prepareCloseBody(code: StatusCodes, reason: string): seq[byte] = result = reason.toBytes if ord(code) > 999: @@ -178,8 +182,7 @@ proc send*( proc send*( ws: WSSession, - data: string): Future[void] - {.raises: [Defect, WSClosedError].} = + data: string): Future[void] = send(ws, data.toBytes(), Opcode.Text) proc handleClose*( @@ -214,7 +217,7 @@ proc handleClose*( else: try: code = StatusCodes(uint16.fromBytesBE(payload[0..<2])) - except RangeError: + except RangeDefect: raise newException(WSInvalidCloseCodeError, "Status code out of range!") @@ -241,6 +244,7 @@ proc handleClose*( try: (code, reason) = ws.onClose(code, reason) except CatchableError as exc: + used(exc) trace "Exception in Close callback, this is most likely a bug", exc = exc.msg else: code = StatusFulfilled @@ -253,6 +257,7 @@ proc handleClose*( try: await ws.send(prepareCloseBody(code, reason), Opcode.Close).wait(5.seconds) except CatchableError as exc: + used(exc) trace "Failed to send Close opcode", err=exc.msg ws.readyState = ReadyState.Closed @@ -303,6 +308,7 @@ proc handleControl*(ws: WSSession, frame: Frame) {.async.} = try: ws.onPing(payload) except CatchableError as exc: + used(exc) trace "Exception in Ping callback, this is most likely a bug", exc = exc.msg # send pong to remote @@ -312,12 +318,15 @@ proc handleControl*(ws: WSSession, frame: Frame) {.async.} = try: ws.onPong(payload) except CatchableError as exc: + used(exc) trace "Exception in Pong callback, this is most likely a bug", exc = exc.msg of Opcode.Close: await ws.handleClose(frame, payload) else: raise newException(WSInvalidOpcodeError, "Invalid control opcode!") +{.warning[HoleEnumConv]:off.} + proc readFrame*(ws: WSSession, extensions: seq[Ext] = @[]): Future[Frame] {.async.} = ## Gets a frame from the WebSocket. ## See https://tools.ietf.org/html/rfc6455#section-5.2 @@ -342,10 +351,11 @@ proc readFrame*(ws: WSSession, extensions: seq[Ext] = @[]): Future[Frame] {.asyn return frame +{.warning[HoleEnumConv]:on.} + proc ping*( ws: WSSession, - data: seq[byte] = @[]): Future[void] - {.raises: [Defect, WSClosedError].} = + data: seq[byte] = @[]): Future[void] = ws.send(data, opcode = Opcode.Ping) proc recv*( @@ -462,7 +472,7 @@ proc recvMsg*( var res: seq[byte] while ws.readyState != ReadyState.Closed: var buf = new(seq[byte]) - let read = await ws.recv(buf, min(size, ws.frameSize)) + let read {.used.} = await ws.recv(buf, min(size, ws.frameSize)) if res.len + buf[].len > size: raise newException(WSMaxMessageSizeError, "Max message size exceeded") @@ -522,7 +532,7 @@ proc close*( except CancelledError as exc: raise exc except CatchableError as exc: - discard # most likely EOF + discard exc # most likely EOF try: ws.readyState = ReadyState.Closing await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds) @@ -530,6 +540,7 @@ proc close*( trace "Cancellation when closing!", exc = exc.msg raise exc except CatchableError as exc: + used(exc) trace "Exception closing", exc = exc.msg finally: await ws.stream.closeWait() diff --git a/websock/types.nim b/websock/types.nim index 41a752dc6a..56187acbdc 100644 --- a/websock/types.nim +++ b/websock/types.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/deques import pkg/[chronos, @@ -73,14 +73,14 @@ type StatusCodes* = distinct range[0..4999] ControlCb* = proc(data: openArray[byte] = []) - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} CloseResult* = tuple code: StatusCodes reason: string CloseCb* = proc(code: StatusCodes, reason: string): - CloseResult {.gcsafe, raises: [Defect].} + CloseResult {.gcsafe, raises: [].} WebSocket* = ref object of RootObj extensions*: seq[Ext] @@ -122,7 +122,7 @@ type ExtFactoryProc* = proc( isServer: bool, args: seq[ExtParam]): Result[Ext, string] - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} ExtFactory* = object name*: string @@ -144,10 +144,10 @@ type Hook* = ref object of RootObj append*: proc(ctx: Hook, headers: var HttpTable): Result[void, string] - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} verify*: proc(ctx: Hook, headers: HttpTable): Future[Result[void, string]] - {.closure, gcsafe, raises: [Defect].} + {.gcsafe, async: (raises: []).} WebSocketError* = object of CatchableError WSMalformedHeaderError* = object of WebSocketError diff --git a/websock/websock.nim b/websock/websock.nim index b90477c418..96317e9181 100644 --- a/websock/websock.nim +++ b/websock/websock.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/[tables, strutils, @@ -38,9 +38,6 @@ type protocols: seq[string] factories: seq[ExtFactory] -func toException(e: string): ref WebSocketError = - (ref WebSocketError)(msg: e) - func toException(e: cstring): ref WebSocketError = (ref WebSocketError)(msg: $e) @@ -57,7 +54,7 @@ proc getFactory(factories: openArray[ExtFactory], extName: string): ExtFactoryPr proc selectExt(isServer: bool, extensions: var seq[Ext], factories: openArray[ExtFactory], - exts: openArray[string]): string {.raises: [Defect, WSExtError].} = + exts: openArray[string]): string {.raises: [WSExtError].} = var extList: seq[AppExt] var response = "" @@ -254,11 +251,12 @@ proc handleRequest*( version: uint = WSDefaultVersion, hooks: seq[Hook] = @[]): Future[WSSession] {. - async, - raises: [ - Defect, + async: + (raises: [ + CancelledError, + CatchableError, WSHandshakeError, - WSProtoMismatchError] + WSProtoMismatchError]) .} = ## Creates a new socket from a request. ##