[PATCH] Use open(O_NONBLOCK) to avoid blocking on FIFOs

Maxim Dounin mdounin at mdounin.ru
Mon Apr 19 04:42:43 MSD 2010


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1271637740 -14400
# Node ID 5616f9bd846b1c3a0e80c64faf2f0176f34513b8
# Parent  a78f3d22264859766f51e50595ad66805029e24d
Use open(O_NONBLOCK) to avoid blocking on FIFOs.

Note that POSIX doesn't specify behaviour of regular files with O_NONBLOCK
flag set, and ideally it should be cleared right after open().  This requires
two extra fcntl() calls (or one ioctl(FIONBIO) call if we cheat and assume
ioctl(FIONBIO) does the same thing as fcntl(O_NONBLOCK)).

On the other hand there are no known OS where O_NONBLOCK actually means
something for regular files.  Igor suggested to use just open(O_NONBLOCK)
without clearing O_NONBLOCK flag until after at least one such OS appears.

Problem was traced down by Vicente Aguilar, see here:

http://nginx.org/pipermail/nginx/2010-February/019004.html
http://nginx.org/pipermail/nginx/2010-March/019094.html

diff --git a/src/core/ngx_open_file_cache.c b/src/core/ngx_open_file_cache.c
--- a/src/core/ngx_open_file_cache.c
+++ b/src/core/ngx_open_file_cache.c
@@ -490,7 +490,8 @@ ngx_open_and_stat_file(u_char *name, ngx
     }
 
     if (!of->log) {
-        fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
+        fd = ngx_open_file(name, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,
+                           NGX_FILE_OPEN, 0);
 
     } else {
         fd = ngx_open_file(name, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN,
diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -64,6 +64,7 @@ typedef struct {
 #define NGX_FILE_OPEN            0
 #define NGX_FILE_TRUNCATE        O_CREAT|O_TRUNC
 #define NGX_FILE_APPEND          O_WRONLY|O_APPEND
+#define NGX_FILE_NONBLOCK        O_NONBLOCK
 
 #define NGX_FILE_DEFAULT_ACCESS  0644
 #define NGX_FILE_OWNER_ACCESS    0600



More information about the nginx-devel mailing list