Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cppdap
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
cppdap
Commits
2f607e07
Commit
2f607e07
authored
Jun 10, 2020
by
Ben Clayton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix deadlock closing socket on another thread
while another thread is stuck in read() or write(). Fixes: #37
parent
9003ee55
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
5 deletions
+58
-5
socket.cpp
src/socket.cpp
+5
-5
socket_test.cpp
src/socket_test.cpp
+53
-0
No files found.
src/socket.cpp
View file @
2f607e07
...
...
@@ -169,20 +169,20 @@ class dap::Socket::Shared : public dap::ReaderWriter {
}
void
close
()
{
#if !defined(_WIN32)
{
RLock
l
(
mutex
);
if
(
s
!=
InvalidSocket
)
{
#if defined(_WIN32)
closesocket
(
s
);
#else
::
shutdown
(
s
,
SHUT_RDWR
);
#endif
}
}
#endif
WLock
l
(
mutex
);
if
(
s
!=
InvalidSocket
)
{
#if defined(_WIN32)
closesocket
(
s
);
#else
#if !defined(_WIN32)
::
close
(
s
);
#endif
s
=
InvalidSocket
;
...
...
src/socket_test.cpp
View file @
2f607e07
...
...
@@ -18,8 +18,61 @@
#include "gtest/gtest.h"
#include <chrono>
#include <thread>
#include <vector>
// Basic socket send & receive test
TEST
(
Socket
,
SendRecv
)
{
const
char
*
port
=
"19021"
;
auto
server
=
dap
::
Socket
(
"localhost"
,
port
);
auto
client
=
dap
::
Socket
::
connect
(
"localhost"
,
port
,
0
);
ASSERT_TRUE
(
client
!=
nullptr
);
const
std
::
string
expect
=
"Hello World!"
;
std
::
string
read
;
auto
thread
=
std
::
thread
([
&
]
{
auto
conn
=
server
.
accept
();
ASSERT_TRUE
(
conn
!=
nullptr
);
char
c
;
while
(
conn
->
read
(
&
c
,
1
)
!=
0
)
{
read
+=
c
;
}
});
ASSERT_TRUE
(
client
->
write
(
expect
.
data
(),
expect
.
size
()));
client
->
close
();
thread
.
join
();
ASSERT_EQ
(
read
,
expect
);
}
// See https://github.com/google/cppdap/issues/37
TEST
(
Socket
,
CloseOnDifferentThread
)
{
const
char
*
port
=
"19021"
;
auto
server
=
dap
::
Socket
(
"localhost"
,
port
);
auto
client
=
dap
::
Socket
::
connect
(
"localhost"
,
port
,
0
);
ASSERT_TRUE
(
client
!=
nullptr
);
auto
conn
=
server
.
accept
();
auto
thread
=
std
::
thread
([
&
]
{
// Closing client on different thread should unblock client->read().
client
->
close
();
});
char
c
;
while
(
client
->
read
(
&
c
,
1
)
!=
0
)
{
}
thread
.
join
();
}
TEST
(
Socket
,
ConnectTimeout
)
{
const
char
*
port
=
"19021"
;
const
int
timeoutMillis
=
200
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment