Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Beyond Protocol
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Beyond AKA
Beyond Protocol
Commits
3df2cd7d
Commit
3df2cd7d
authored
2 years ago
by
Nicolas Pope
Browse files
Options
Downloads
Patches
Plain Diff
Try without peer locks
parent
44e7b3e2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/peer.cpp
+33
-33
33 additions, 33 deletions
src/peer.cpp
with
33 additions
and
33 deletions
src/peer.cpp
+
33
−
33
View file @
3df2cd7d
...
...
@@ -258,7 +258,7 @@ NodeType Peer::getType() const {
}
void
Peer
::
data
()
{
UNIQUE_LOCK
(
recv_mtx_
,
lk
);
//
UNIQUE_LOCK(recv_mtx_,lk);
if
(
!
sock_
->
is_valid
())
{
return
;
}
...
...
@@ -278,8 +278,8 @@ void Peer::data() {
// outside the lock and the second thread to call recv() re-acquires
// the lock first, buffer_consumed() will be called first with second
// thread's number of bytes (rc).
auto
ctr
=
dbg_recv_begin_ctr_
++
;
lk
.
unlock
();
//
auto ctr = dbg_recv_begin_ctr_++;
//
lk.unlock();
try
{
rc
=
sock_
->
recv
(
recv_buf_
.
buffer
(),
recv_buf_
.
buffer_capacity
());
...
...
@@ -312,17 +312,17 @@ void Peer::data() {
}
// Re-acquire lock before processing buffer further
lk
.
lock
();
//
lk.lock();
// buffer_consumed() will not be updated with correct value, race condition
// described above has occurred
CHECK
(
ctr
==
dbg_recv_end_ctr_
++
)
<<
"race in Peer::data()"
;
//
CHECK(ctr == dbg_recv_end_ctr_++) << "race in Peer::data()";
recv_buf_
.
buffer_consumed
(
rc
);
if
(
is_waiting_
)
{
is_waiting_
=
false
;
lk
.
unlock
();
//
lk.unlock();
++
job_count_
;
...
...
@@ -356,7 +356,7 @@ bool Peer::_has_next() {
bool
Peer
::
_data
()
{
// lock before trying to acquire handle to buffer
UNIQUE_LOCK
(
recv_mtx_
,
lk
);
//
UNIQUE_LOCK(recv_mtx_, lk);
// msgpack::object is valid as long as handle is
msgpack
::
object_handle
msg_handle
;
...
...
@@ -373,27 +373,16 @@ bool Peer::_data() {
return
false
;
}
lk
.
unlock
();
//
lk.unlock();
msgpack
::
object
obj
=
msg_handle
.
get
();
// more data: repeat (loop)
++
job_count_
;
ftl
::
pool
.
push
([
this
](
int
id
)
{
try
{
_data
();
}
catch
(
const
std
::
exception
&
e
)
{
LOG
(
ERROR
)
<<
"Error processing packet: "
<<
e
.
what
();
}
--
job_count_
;
});
if
(
status_
==
NodeStatus
::
kConnecting
)
{
// If not connected, must lock to make sure no other thread performs this step
lk
.
lock
();
//
lk.lock();
// Verify still not connected after lock
if
(
status_
==
NodeStatus
::
kConnecting
)
{
//
if (status_ == NodeStatus::kConnecting) {
// First message must be a handshake
try
{
tuple
<
uint32_t
,
std
::
string
,
msgpack
::
object
>
hs
;
...
...
@@ -403,26 +392,26 @@ bool Peer::_data() {
LOG
(
WARNING
)
<<
"Missing handshake - got '"
<<
get
<
1
>
(
hs
)
<<
"'"
;
// Allow a small delay in case another thread is doing the handshake
lk
.
unlock
();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
if
(
status_
==
NodeStatus
::
kConnecting
)
{
//
lk.unlock();
//
std::this_thread::sleep_for(std::chrono::milliseconds(10));
//
if (status_ == NodeStatus::kConnecting) {
LOG
(
ERROR
)
<<
"failed to get handshake"
;
close
(
reconnect_on_protocol_error_
);
lk
.
lock
();
//
lk.lock();
return
false
;
}
//
}
}
else
{
// Must handle immediately with no other thread able
// to read next message before completion.
// The handshake handler must not block.
disp_
->
dispatch
(
*
this
,
obj
);
return
true
;
//
disp_->dispatch(*this, obj);
//
return true;
}
}
catch
(...)
{
LOG
(
WARNING
)
<<
"Bad first message format... waiting"
;
// Allow a small delay in case another thread is doing the handshake
lk
.
unlock
();
//
lk.unlock();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
if
(
status_
==
NodeStatus
::
kConnecting
)
{
LOG
(
ERROR
)
<<
"failed to get handshake"
;
...
...
@@ -430,16 +419,27 @@ bool Peer::_data() {
return
false
;
}
}
}
else
{
lk
.
unlock
();
}
//
} else {
//
lk.unlock();
//
}
}
// more data: repeat (loop)
++
job_count_
;
ftl
::
pool
.
push
([
this
](
int
id
)
{
try
{
_data
();
}
catch
(
const
std
::
exception
&
e
)
{
LOG
(
ERROR
)
<<
"Error processing packet: "
<<
e
.
what
();
}
--
job_count_
;
});
disp_
->
dispatch
(
*
this
,
obj
);
// Lock again before freeing msg_handle (destruction order).
// msgpack::object_handle destructor modifies recv_buffer_
lk
.
lock
();
//
lk.lock();
return
true
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment