From 19444825fabe4e937ea398f72dbf8468a34bd29a Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 10 Sep 2018 10:58:50 +0100 Subject: [PATCH] Actually report errors when hitting blacklisted errnos. (#610) Motivation: It's not all that useful to print the value of the pointer from sterror. Instead we should aim to print the string. While we're here, let's avoid repeating too much code. Modifications: - Centralised the handling of blacklisted errnos. - Correctly printed the error string when a blacklisted errno is hit. - Updated the output of the assertion to include the function failing. Result: Better diagnostics when blacklisted errnos are hit. --- Sources/NIO/System.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/NIO/System.swift b/Sources/NIO/System.swift index aa12a31a8..a118ec837 100644 --- a/Sources/NIO/System.swift +++ b/Sources/NIO/System.swift @@ -77,6 +77,10 @@ private func isBlacklistedErrno(_ code: Int32) -> Bool { } } +private func assertIsNotBlacklistedErrno(err: CInt, where function: StaticString) -> Void { + assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(String(cString: strerror(err)!)) in \(function))") +} + /* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */ @inline(__always) internal func wrapSyscallMayBlock(where function: StaticString = #function, _ body: () throws -> T) throws -> IOResult { @@ -90,7 +94,7 @@ internal func wrapSyscallMayBlock(where function: StaticSt case EWOULDBLOCK: return .wouldBlock(0) default: - assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") + assertIsNotBlacklistedErrno(err: err, where: function) throw IOError(errnoCode: err, function: function) } @@ -109,7 +113,7 @@ internal func wrapSyscall(where function: StaticString = # if err == EINTR { continue } - assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") + assertIsNotBlacklistedErrno(err: err, where: function) throw IOError(errnoCode: err, function: function) } return res @@ -125,7 +129,7 @@ internal func wrapErrorIsNullReturnCall(where function: StaticString = #funct if err == EINTR { continue } - assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") + assertIsNotBlacklistedErrno(err: err, where: function) throw IOError(errnoCode: err, function: function) } return res @@ -202,7 +206,7 @@ internal enum Posix { // - https://bugs.chromium.org/p/chromium/issues/detail?id=269623 // - https://lwn.net/Articles/576478/ if err != EINTR { - assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") + assertIsNotBlacklistedErrno(err: err, where: #function) throw IOError(errnoCode: err, function: "close") } }