--- src/rrd_open.c.orig	2026-05-19 19:35:26 UTC
+++ src/rrd_open.c
@@ -33,6 +33,16 @@
 #include "rrd_rados.h"
 #endif
 
+#include <signal.h>
+/*
+ * Signal handler for SIGALRM.
+ */
+static void
+timeout(int sig)
+{
+    (void)sig;
+}
+
 #define MEMBLK 8192
 
 #ifdef _WIN32
@@ -799,8 +809,9 @@ int rrd_rwlock(
             return 0;
     }
 #endif
-    int       rcstat;
+    int       rcstat, waitsec;
     rrd_simple_file_t *rrd_simple_file;
+    char     *endptr, *pwaitsec;
 
     rrd_simple_file = (rrd_simple_file_t *) rrd_file->pvt;
 #ifdef USE_WINDOWS_LOCK
@@ -809,6 +820,25 @@ int rrd_rwlock(
     /* Silence unused parameter compiler warning */
     (void) writelock;
 #else
+    if ((pwaitsec = getenv("RRDTOOL_LOCK_TIMEOUT")) != NULL) {
+	waitsec = strtol(pwaitsec, &endptr, 0);
+	if (*endptr == '\0' && waitsec >= 0) {
+	    if (waitsec > 0) {			/* Set up a timeout. */
+	        struct sigaction act;
+
+	        act.sa_handler = timeout;
+	        sigemptyset(&act.sa_mask);
+	        act.sa_flags = SA_RESETHAND;	/* Note that we do not set SA_RESTART. */
+	        sigaction(SIGALRM, &act, NULL);
+	        alarm(waitsec);
+	    }
+	    rcstat = flock(rrd_simple_file->fd, writelock ? LOCK_EX : LOCK_SH);
+	    if (waitsec > 0)
+		alarm(0);
+
+	    return (rcstat);
+	}
+    }
     {
         struct flock lock;
         int op = lock_mode == RRD_LOCK_TRY ? F_SETLK : F_SETLKW;

--- doc/rrdtool.pod.orig	2026-05-19 19:34:20 UTC
+++ doc/rrdtool.pod
@@ -356,6 +356,21 @@ stress on your disks.
 L<rrdcached>, a caching daemon for RRDtool which may help you lessen the
 stress on your disks.
 
+=head1 ENVIRONMENT
+
+=over 8
+
+=item RRDTOOL_LOCK_TIMEOUT
+
+By default, B<RRDtool> tries to lock RRD file and fails
+if it cannot obtain the lock immediately.
+This variable allows to change this behavior and specify
+a time interval in seconds to wait for lock if the file is busy.
+It will fail if the lock cannot be obtained in time.
+Zero value makes it wait for the lock indefinitely.
+
+=back
+
 =head1 SEE ALSO
 
 rrdcreate, rrdupdate, rrdgraph, rrddump, rrdfetch, rrdtune, rrdlast, rrdxport,
