Skip to content

Commit

Permalink
move signal ignore out of eth/common
Browse files Browse the repository at this point in the history
`net/nat` is the only place where it is used and it certainly doesn't
belong in `common`
  • Loading branch information
arnetheduck committed Sep 24, 2024
1 parent 410eab5 commit e62de6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
35 changes: 1 addition & 34 deletions eth/common/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,13 @@

import
std/[hashes],
nimcrypto/hash, stew/byteutils, metrics,
nimcrypto/hash, stew/byteutils,
./eth_types

when defined(posix):
import std/[posix, os]

export metrics

proc hash*(d: MDigest): Hash {.inline.} = hash(d.data)

proc parseAddress*(hexString: string): EthAddress =
hexToPaddedByteArray[20](hexString)

proc `$`*(a: EthAddress): string =
a.toHex()

# Block all/most signals in the current thread, so we don't interfere with regular signal
# handling elsewhere.
proc ignoreSignalsInThread*() =
when defined(posix):
var signalMask, oldSignalMask: Sigset

# sigprocmask() doesn't work on macOS, for multithreaded programs
if sigfillset(signalMask) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)
when defined(boehmgc):
# Turns out Boehm GC needs some signals to deal with threads:
# https://www.hboehm.info/gc/debugging.html
const
SIGPWR = 30
SIGXCPU = 24
SIGSEGV = 11
SIGBUS = 7
if sigdelset(signalMask, SIGPWR) != 0 or
sigdelset(signalMask, SIGXCPU) != 0 or
sigdelset(signalMask, SIGSEGV) != 0 or
sigdelset(signalMask, SIGBUS) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)
if pthread_sigmask(SIG_BLOCK, signalMask, oldSignalMask) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)
31 changes: 31 additions & 0 deletions eth/net/nat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ var
logScope:
topics = "eth net nat"

when defined(posix):
import std/posix

# Block all/most signals in the current thread, so we don't interfere with regular signal
# handling elsewhere.
proc ignoreSignalsInThread*() =
when defined(posix):
var signalMask, oldSignalMask: Sigset

# sigprocmask() doesn't work on macOS, for multithreaded programs
if sigfillset(signalMask) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)
when defined(boehmgc):
# Turns out Boehm GC needs some signals to deal with threads:
# https://www.hboehm.info/gc/debugging.html
const
SIGPWR = 30
SIGXCPU = 24
SIGSEGV = 11
SIGBUS = 7
if sigdelset(signalMask, SIGPWR) != 0 or
sigdelset(signalMask, SIGXCPU) != 0 or
sigdelset(signalMask, SIGSEGV) != 0 or
sigdelset(signalMask, SIGBUS) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)
if pthread_sigmask(SIG_BLOCK, signalMask, oldSignalMask) != 0:
echo osErrorMsg(osLastError())
quit(QuitFailure)

## Also does threadvar initialisation.
## Must be called before redirectPorts() in each thread.
proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Opt[IpAddress] =
Expand Down

0 comments on commit e62de6f

Please sign in to comment.