From a2f63bfec044f3bb64d984f649fb757c69b64fe9 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Thu, 13 Apr 2023 14:27:09 +0100 Subject: [PATCH] use non-blocking mode to accept connections --- ChangeLog | 7 +++++++ Source/NSSocketPort.m | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/ChangeLog b/ChangeLog index b9d8dc9ba..e55d27f3d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2022-04-13 Richard Frith-Macdonald + + * Source/NSSocketPort.m: set listening socket into non-blocking mode + if possible, so if multiple threads are using the same port we don't + get one thread blocked wating for an accept which was handled by + another one. + 2022-03-23 Richard Frith-Macdonald * Source/NSTimer.m: wrap call to block in exception handler (bug #289) diff --git a/Source/NSSocketPort.m b/Source/NSSocketPort.m index 953b8fc83..ea6523d3a 100644 --- a/Source/NSSocketPort.m +++ b/Source/NSSocketPort.m @@ -1864,6 +1864,23 @@ static Class tcpPortClass; { #if defined(_WIN32) int rc; + +#else + int e; + if ((e = fcntl(desc, F_GETFL, 0)) >= 0) + { + e |= NBLK_OPT; + if (fcntl(desc, F_SETFL, e) < 0) + { + NSLog(@"unable to set non-blocking mode on %d - %@", + desc, [NSError _last]); + } + } + else + { + NSLog(@"unable to get/set non-blocking mode on %d - %@", + desc, [NSError _last]); + } #endif /* * Set up the listening descriptor and the actual TCP port