Skip to content

Commit

Permalink
add mutex to make tsan happy (#383)
Browse files Browse the repository at this point in the history
* add mutex to make tsan happy

* Remove race condition in test
  • Loading branch information
MisterTea authored Jan 29, 2021
1 parent 3edd879 commit 5cfa137
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/base/SocketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SocketHandler {

template <typename T>
inline void writeProto(int fd, const T& t, bool timeout) {
lock_guard<std::mutex> guard(writeMutex);
string s;
if (!t.SerializeToString(&s)) {
STFATAL << "Serialization of " << t.GetTypeName() << " failed!";
Expand Down Expand Up @@ -119,6 +120,10 @@ class SocketHandler {
virtual void stopListening(const SocketEndpoint& endpoint) = 0;
virtual void close(int fd) = 0;
virtual vector<int> getActiveSockets() = 0;

protected:
// This mutex only exists to remove a tsan error in integration tests
mutex writeMutex;
};
} // namespace et

Expand Down
12 changes: 4 additions & 8 deletions test/TerminalTest.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "TestHeaders.hpp"

#include "FakeConsole.hpp"
#include "TerminalClient.hpp"
#include "TerminalServer.hpp"
#include "TestHeaders.hpp"

namespace et {
TEST_CASE("FakeConsoleTest", "[FakeConsoleTest]") {
Expand Down Expand Up @@ -69,8 +68,6 @@ TEST_CASE("FakeUserTerminalTest", "[FakeUserTerminalTest]") {
thread t2([fakeUserTerminal, s]() {
fakeUserTerminal->simulateTerminalResponse(s);
});
usleep(1000);
REQUIRE(socketHandler->hasData(fakeUserTerminal->getFd()));

string s3(64 * 1024, '\0');
socketHandler->readAll(fakeUserTerminal->getFd(), &s3[0], s3.length(), false);
Expand Down Expand Up @@ -102,8 +99,7 @@ void readWriteTest(const string& clientId,
shared_ptr<TerminalClient> terminalClient(new TerminalClient(
clientSocketHandler, clientPipeSocketHandler, serverEndpoint, clientId,
CRYPTO_KEY, fakeConsole, false, "", "", false, ""));
thread terminalClientThread(
[terminalClient]() { terminalClient->run(""); });
thread terminalClientThread([terminalClient]() { terminalClient->run(""); });
sleep(3);

string s(1024, '\0');
Expand Down Expand Up @@ -184,8 +180,8 @@ TEST_CASE("EndToEndTest", "[EndToEndTest]") {
sleep(1);

readWriteTest("1234567890123456", routerSocketHandler, fakeUserTerminal,
serverEndpoint, clientSocketHandler, clientPipeSocketHandler, fakeConsole,
routerEndpoint);
serverEndpoint, clientSocketHandler, clientPipeSocketHandler,
fakeConsole, routerEndpoint);
server->shutdown();
t_server.join();

Expand Down

0 comments on commit 5cfa137

Please sign in to comment.