git: f44a448709d3 - main - fusefs: fix intermittency in the dev_fuse_poll test

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 06 Oct 2021 20:29:03 UTC
The branch main has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=f44a448709d3b77508fd59ee28201ae1666387c2

commit f44a448709d3b77508fd59ee28201ae1666387c2
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2021-09-25 16:16:20 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2021-10-06 20:28:48 +0000

    fusefs: fix intermittency in the dev_fuse_poll test
    
    The DevFusePoll::access/select test would occasionally segfault.  The
    cause was a file descriptor that was shared between two threads.  The
    first thread would kill the second and close the file descriptor.  But
    it was possible that the second would read the file descriptor before it
    shut down.  That did not cause problems for kqueue, poll, or blocking
    operation, but it triggered segfaults in select's macros.
    
    MFC after:      2 weeks
    Differential Revision: https://reviews.freebsd.org/D32142
---
 tests/sys/fs/fusefs/mockfs.cc | 10 +++++++---
 tests/sys/fs/fusefs/mockfs.hh |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/tests/sys/fs/fusefs/mockfs.cc b/tests/sys/fs/fusefs/mockfs.cc
index 4d0724b9c227..99f7ccc61273 100644
--- a/tests/sys/fs/fusefs/mockfs.cc
+++ b/tests/sys/fs/fusefs/mockfs.cc
@@ -870,6 +870,7 @@ void MockFS::read_request(mockfs_buf_in &in, ssize_t &res) {
 	struct timeval timeout_tv;
 	const int timeout_ms = 999;
 	int timeout_int, nfds;
+	int fuse_fd;
 
 	switch (m_pm) {
 	case BLOCKING:
@@ -906,19 +907,22 @@ void MockFS::read_request(mockfs_buf_in &in, ssize_t &res) {
 		ASSERT_TRUE(fds[0].revents & POLLIN);
 		break;
 	case SELECT:
+		fuse_fd = m_fuse_fd;
+		if (fuse_fd < 0)
+			break;
 		timeout_tv.tv_sec = 0;
 		timeout_tv.tv_usec = timeout_ms * 1'000;
-		nfds = m_fuse_fd + 1;
+		nfds = fuse_fd + 1;
 		while (nready == 0) {
 			FD_ZERO(&readfds);
-			FD_SET(m_fuse_fd, &readfds);
+			FD_SET(fuse_fd, &readfds);
 			nready = select(nfds, &readfds, NULL, NULL,
 				&timeout_tv);
 			if (m_quit)
 				return;
 		}
 		ASSERT_LE(0, nready) << strerror(errno);
-		ASSERT_TRUE(FD_ISSET(m_fuse_fd, &readfds));
+		ASSERT_TRUE(FD_ISSET(fuse_fd, &readfds));
 		break;
 	default:
 		FAIL() << "not yet implemented";
diff --git a/tests/sys/fs/fusefs/mockfs.hh b/tests/sys/fs/fusefs/mockfs.hh
index 24ca017dcdb1..600a4b4292c0 100644
--- a/tests/sys/fs/fusefs/mockfs.hh
+++ b/tests/sys/fs/fusefs/mockfs.hh
@@ -284,7 +284,7 @@ class MockFS {
 	pthread_t m_daemon_id;
 
 	/* file descriptor of /dev/fuse control device */
-	int m_fuse_fd;
+	volatile int m_fuse_fd;
 	
 	/* The minor version of the kernel API that this mock daemon targets */
 	uint32_t m_kernel_minor_version;