This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch flock
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 0322f395ff2c39de704c173b6fae31fb53a7956f
Author: Robert Newson <[email protected]>
AuthorDate: Thu Sep 24 21:07:57 2026 +0100

    take exclusive lock on all couch_cfiles
---
 src/couch/priv/couch_cfile/couch_cfile.c | 9 ++++++++-
 src/couch/src/couch_file.erl             | 4 ++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/couch/priv/couch_cfile/couch_cfile.c 
b/src/couch/priv/couch_cfile/couch_cfile.c
index c7e6f16f0..efd9e17eb 100644
--- a/src/couch/priv/couch_cfile/couch_cfile.c
+++ b/src/couch/priv/couch_cfile/couch_cfile.c
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <sys/uio.h>
 #include <unistd.h>
+#include <sys/file.h>
 
 // erl_driver.h is for erl_errno_id()
 #include "erl_driver.h"
@@ -234,7 +235,7 @@ int efile_datasync(int fd, posix_errno_t* res_errno) {
 static ERL_NIF_TERM dup_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM 
argv[])
 {
 #ifdef COUCH_CFILE_SUPPORTED
-   int fd, newfd;
+   int fd, newfd, flock_res;
    handle_t* hdl;
    ErlNifRWLock *lock;
    ErlNifPid pid;
@@ -257,6 +258,12 @@ static ERL_NIF_TERM dup_nif(ErlNifEnv *env, int argc, 
const ERL_NIF_TERM argv[])
    if (newfd < 0) {
        return err_tup(env, errno);
    }
+
+   flock_res = flock(newfd, LOCK_EX | LOCK_NB);
+   if (flock_res == -1) {
+     return err_tup(env, errno);
+   }
+
    // From here on close the new dup-ed file descriptors on any failure.
 
    lock = enif_rwlock_create("couch_cfile:rwlock");
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index 1a69fa017..9e9ff924b 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -1020,8 +1020,8 @@ dup(#file{fd = Fd} = File) ->
                     % Use an effective infinity for eof max limit for now
                     put(?CFILE_HANDLE, CFile#file{eof = 1 bsl 60}),
                     CFile;
-                {error, _Error} ->
-                    File
+                {error, Error} ->
+                    throw({stop, {error, Error}})
             end;
         false ->
             File

Reply via email to