Jdesk / memcached

Automatically exported from code.google.com/p/memcached
0 stars 0 forks source link

Superfluous calls to fcntl to get/set no-blocking mode #85

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
On systems using BSD sockets the socket returned by accept inherits the
properties from the socket you accept from. 

Linux implements their socket layer a bit differently by _not_ inheriting
the properties.

We could add an ifdef __linux around the code block, but then we wouldn't
detect new platforms with a similar behavior on accept. The following code
snippet could be used, but it doesn't actually simplify the code.

#ifdef __linux
            /* The socket created by accept() will inherit the properties
             * from c->sfd on systems implementing BSD sockets. 
             * The network stack on Linux works differently and doesn't
             * inherit any properties.
             */
            int flags;
            if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 ||
                fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
                perror("setting O_NONBLOCK");
                close(sfd);
                break;
            }
#elif !defined(NDEBUG)
            int flags;
            assert((flags = fcntl(sfd, F_GETFL, 0)) != -1 &&
                   (flags & O_NONBLOCK) == O_NONBLOCK);

#endif

I suggest that we leave the code as it is. The primary motivation for
filing the bug was to simplify the code, and this is not part of the
"critical path". You shouldn't be using short-lived connections in the
first place. 

Original issue reported on code.google.com by trond.no...@gmail.com on 28 Aug 2009 at 8:24