Merge pull request #2484 from yglukhov/macos-sigpipe

Fixed SIGPIPE on MacOS
This commit is contained in:
Andreas Rumpf
2015-04-08 16:03:32 +02:00
3 changed files with 11 additions and 6 deletions

View File

@@ -1580,8 +1580,11 @@ else:
when defined(macosx):
# We can't use the NOSIGNAL flag in the ``send`` function, it has no effect
var
# Instead we should use SO_NOSIGPIPE in setsockopt
const
MSG_NOSIGNAL* = 0'i32
var
SO_NOSIGPIPE* {.importc, header: "<sys/socket.h>".}: cint
else:
var
MSG_NOSIGNAL* {.importc, header: "<sys/socket.h>".}: cint

View File

@@ -841,6 +841,8 @@ else:
proc newAsyncRawSocket*(domain: cint, typ: cint, protocol: cint): TAsyncFD =
result = newRawSocket(domain, typ, protocol).TAsyncFD
result.SocketHandle.setBlocking(false)
when defined(macosx):
result.SocketHandle.setSockOptInt(SOL_SOCKET, SO_NOSIGPIPE, 1)
register(result)
proc newAsyncRawSocket*(domain: Domain = AF_INET,
@@ -848,6 +850,8 @@ else:
protocol: Protocol = IPPROTO_TCP): TAsyncFD =
result = newRawSocket(domain, typ, protocol).TAsyncFD
result.SocketHandle.setBlocking(false)
when defined(macosx):
result.SocketHandle.setSockOptInt(SOL_SOCKET, SO_NOSIGPIPE, 1)
register(result)
proc closeSocket*(sock: TAsyncFD) =
@@ -959,7 +963,6 @@ else:
result = true
let res = recv(sock.SocketHandle, addr readBuffer[0], size.cint,
flags.toOSFlags())
#echo("recv cb res: ", res)
if res < 0:
let lastError = osLastError()
if lastError.int32 notin {EINTR, EWOULDBLOCK, EAGAIN}:

View File

@@ -39,6 +39,9 @@ export
SO_KEEPALIVE, SO_OOBINLINE, SO_REUSEADDR,
MSG_PEEK
when defined(macosx):
export SO_NOSIGPIPE
type
Port* = distinct uint16 ## port type
@@ -428,10 +431,6 @@ proc selectWrite*(writefds: var seq[SocketHandle],
pruneSocketSet(writefds, (wr))
# We ignore signal SIGPIPE on Darwin
when defined(macosx) and not defined(nimdoc):
signal(SIGPIPE, SIG_IGN)
when defined(Windows):
var wsa: WSAData
if wsaStartup(0x0101'i16, addr wsa) != 0: raiseOSError(osLastError())