Skip to content

Commit abea2b2

Browse files
committed
eh
1 parent 4796607 commit abea2b2

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/system/excpt.nim

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const noStacktraceAvailable = "No stack traceback available\n"
1717

1818
var
1919
errorMessageWriter*: (proc(msg: string) {.tags: [WriteIOEffect], benign,
20-
nimcall.})
20+
nimcall, raises: [].})
2121
## Function that will be called
2222
## instead of `stdmsg.write` when printing stacktrace.
2323
## Unstable API.
@@ -632,7 +632,7 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
632632
# for libbacktrace.
633633
var sigHandlerBuf = newStringOfCap(32*1024)
634634

635-
proc signalHandler(sign: cint) {.exportc: "signalHandler", noconv.} =
635+
proc signalHandler(sign: cint) {.exportc: "signalHandler", noconv, raises: [].} =
636636
template processSignal(s, action: untyped) {.dirty.} =
637637
if s == SIGINT: action("SIGINT: Interrupted by Ctrl-C.\n")
638638
elif s == SIGSEGV:
@@ -654,7 +654,12 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
654654
# print stack trace and quit
655655
when defined(memtracker):
656656
logPendingOps()
657-
when hasSomeStackTrace:
657+
# On windows, it is common that the signal handler is called from a non-Nim
658+
# thread - until `rawWriteStackTrace` and friends learns to not allocate
659+
# memory, we'll skip the memory allocation and avoid crashes this way
660+
# On other platforms, the memory allocation may still cause crashes, but
661+
# not as frequently. YOLO.
662+
when hasSomeStackTrace and not defined(windows):
658663
when not usesDestructors: GC_disable()
659664
rawWriteStackTrace(sigHandlerBuf)
660665
processSignal(sign, sigHandlerBuf.add) # nice hu? currying a la Nim :-)
@@ -665,9 +670,11 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
665670
template asgn(y) =
666671
msg = y
667672
processSignal(sign, asgn)
668-
# xxx use string for msg instead of cstring, and here use showErrorMessage2(msg)
669-
# unless there's a good reason to use cstring in signal handler to avoid
670-
# using gc?
673+
# showErrorMessage may allocate, which may cause a crash, and calls C
674+
# library functions which is undefined behavior, ie it may also crash.
675+
# Nevertheless, we sometimes manage to emit the message regardless which
676+
# pragmatically makes this attempt "useful enough".
677+
# See also https://en.cppreference.com/w/c/program/signal
671678
showErrorMessage(msg, msg.len)
672679

673680
when defined(posix):

lib/system/memtracker.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type
3535
count*: int
3636
disabled: bool
3737
data*: array[400, LogEntry]
38-
TrackLogger* = proc (log: TrackLog) {.nimcall, tags: [], gcsafe.}
38+
TrackLogger* = proc (log: TrackLog) {.nimcall, tags: [], gcsafe, raises: [].}
3939

4040
var
4141
gLog*: TrackLog

0 commit comments

Comments
 (0)