From 73fb994ad70cb4930f6cf1fada9f60ca155dc62b Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Fri, 10 Apr 2020 05:34:12 +0000
Subject: [PATCH] If smb_getstatus() fails with a SMB_ERR_READ (-203) error
 while trying to add a new message with smb_addmsg(), retry (using the
 configured delay/timeout values). On Windows, importing a QWK packet into SMB
 messagebases stored on a remote Samba server, for some unknown reason, the
 read of the msgbase status header here would fail about 1 out of 100 times,
 with the error:  evnt ERROR 13 (Permission denied) (WinError 33) in
 qwktomsg.cpp line 515  (sbbs_t::qwk_import_msg) writing "msgbase" access=-203
 info=smb_getstatus  reading status ... as if another task had the msgbase
 status hdr locked (which, they don't), and we (this thread) already had it
 locked (just 8 lines up in this same function), so shouldn't even be
 possible. A single retry appeared to be sufficient, but I went ahead and put
 the timed-loop with the delay in here.

---
 src/smblib/smbadd.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/smblib/smbadd.c b/src/smblib/smbadd.c
index 710d0e4581..7160f857fb 100644
--- a/src/smblib/smbadd.c
+++ b/src/smblib/smbadd.c
@@ -59,6 +59,7 @@ int SMBCALL smb_addmsg(smb_t* smb, smbmsg_t* msg, int storage, long dupechk_hash
 	hash_t		found;
 	hash_t**	hashes=NULL;	/* This is a NULL-terminated list of hashes */
 	smbmsg_t	remsg;
+	time_t		start = 0;
 
 	if(!SMB_IS_OPEN(smb)) {
 		safe_snprintf(smb->last_error,sizeof(smb->last_error),"%s msgbase not open", __FUNCTION__);
@@ -79,7 +80,15 @@ int SMBCALL smb_addmsg(smb_t* smb, smbmsg_t* msg, int storage, long dupechk_hash
 	/* try */
 	do {
 
-		if((retval=smb_getstatus(smb))!=SMB_SUCCESS)
+		while((retval=smb_getstatus(smb)) == SMB_ERR_READ) {
+			if(!start)
+				start=time(NULL);
+			else
+				if(time(NULL)-start>=(time_t)smb->retry_time)
+					break; 
+			SLEEP(smb->retry_delay);
+		}
+		if(retval != SMB_SUCCESS)
 			break;
 
 		msg->hdr.number=smb->status.last_msg+1;
-- 
GitLab