-
Notifications
You must be signed in to change notification settings - Fork 714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition when using ResponseStream::flush() from another thread #1290
Comments
Hi @bkueng - Thanks for reporting this issue. Couple of things. I'd be curious to see the full stack backtrace for both the Because Are you using any other means of sending a response, e.g. calling send(...) at all?
[DG] I don't know that any more info is needed beyond the sanitizer report, but if you do want Pistache's internal debug logs you can use the meson setup options:
[DG] It is certainly more common to use Thanks again! |
I was not able to trigger it with full debug build, unfortunately. My setup is similar as in the So to test further I added a lock around all the I captured a full stack trace for that:
The other call from I was able to resolve the problem with these changes (I'm not suggesting this is how it should be solved though): diff --git a/include/pistache/transport.h b/include/pistache/transport.h
index d406ae1..6e262e1 100644
--- a/include/pistache/transport.h
+++ b/include/pistache/transport.h
@@ -78,6 +78,7 @@ namespace Pistache::Tcp
msg_more_style
#endif
);
+ Guard guard(writesQueueLock);
writesQueue.push(std::move(write));
});
}
@@ -261,6 +262,7 @@ namespace Pistache::Tcp
std::shared_ptr<EventMethEpollEquiv> epoll_fd;
#endif
+ Lock writesQueueLock;
PollableQueue<WriteEntry> writesQueue;
std::unordered_map<Fd, std::deque<WriteEntry>> toWrite;
Lock toWriteLock;
diff --git a/src/common/transport.cc b/src/common/transport.cc
index f9a3fe8..d78c23b 100644
--- a/src/common/transport.cc
+++ b/src/common/transport.cc
@@ -968,6 +968,7 @@ namespace Pistache::Tcp
// Let's drain the queue
for (;;)
{
+ Guard guard(writesQueueLock);
auto write = writesQueue.popSafe();
if (!write)
break; Hence I think it needs a change in the |
Thanks, this is helpful data. Some sort of lock, as you described, could work. I'd also like to ponder whether response-flush could bypass the write queue completely, thus avoiding the issue of a lock on the queue. But I will need some more time to review that. It would still be helpful to have the full stack trace you see for the Thanks once more. |
Thanks for the quick reply.
I was also thinking in that direction, to let the epoll worker thread do the actual work. But I don't have enough knowledge about the library to propose something concrete.
Yes, like this one (the
I don't need a solution in pistache immediately, for now I can use the added lock, my application isn't performance-critical. |
Perfect, thanks. I will look for a solution here when I have the chance/time. |
…1290 http.cc Remove the call to transport_->flush in ResponseStream::flush. The call was creating corruption in writesQueue as per Issue pistacheio#1290. See code comment for more details.
Hi @bkueng, I made a branch on my personal Pistache fork that attempts to address this issue. I wonder if you could try it out? Alternatively, if it's easier for you to edit your own Pistache source, please just comment out the call I'd be grateful to hear back whether the above change fixes the issue in your scenario, and also doesn't cause any other issues. Thanks in advance! |
I have a use-case where I call
ResponseStream::flush()
from another thread for an ongoing connection.In rare cases ThreadSanitizer raises an issue in the form of:
There's somehow no debug info for the pistache lib, but this is the full stack when tsan does the reporting:
Is my usage pattern not supported? Is there another approach?
From what I see it is violating the 'single consumer' assumption of the
Pistache::Queue
.Just to test I added a mutex around the writesQueue in transport.h accesses, which silences the sanitizer.
I'm using commit 5d8d062, very close to master.
The text was updated successfully, but these errors were encountered: