Skip to content
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

fix(network): set deadline for streams #1149

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions network/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
}

func newStreamService(ctx context.Context, host lp2phost.Host,
protocolID lp2pcore.ProtocolID,
eventCh chan Event, log *logger.SubLogger,
protocolID lp2pcore.ProtocolID, eventCh chan Event, log *logger.SubLogger,
) *streamService {
s := &streamService{
ctx: ctx,
Expand Down Expand Up @@ -65,7 +64,7 @@
}

// To prevent a broken stream from being open forever.
ctxWithTimeout, cancel := context.WithTimeout(s.ctx, 2*time.Second)
ctxWithTimeout, cancel := context.WithTimeout(s.ctx, 1*time.Minute)
defer cancel()

// Attempt to open a new stream to the target peer assuming there's already direct a connection
Expand All @@ -75,10 +74,16 @@
return LibP2PError{Err: err}
}

deadline, _ := ctxWithTimeout.Deadline()
_ = stream.SetDeadline(deadline)

_, err = stream.Write(msg)
if err != nil {
_ = stream.Reset()

Check warning on line 82 in network/stream.go

View check run for this annotation

Codecov / codecov/patch

network/stream.go#L82

Added line #L82 was not covered by tests

return LibP2PError{Err: err}
}

err = stream.CloseWrite()
if err != nil {
return LibP2PError{Err: err}
Expand Down
2 changes: 1 addition & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@

err := sync.network.SendTo(data, to)
if err != nil {
sync.logger.Debug("error on sending the bundle, closing the connection",
sync.logger.Warn("error on sending the bundle, closing the connection",

Check warning on line 171 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L171

Added line #L171 was not covered by tests
"bundle", bdl, "to", to, "error", err)

sync.network.CloseConnection(to)
Expand Down
Loading