Skip to content

Commit

Permalink
fix bug of writing after disconnected. sendInLoop() could be queued a…
Browse files Browse the repository at this point in the history
…nd called after handleClose().
  • Loading branch information
chenshuo committed Dec 10, 2012
1 parent 87b372a commit dd26871
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions muduo/net/TcpConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ void TcpConnection::sendInLoop(const void* data, size_t len)
loop_->assertInLoopThread();
ssize_t nwrote = 0;
size_t remaining = len;
bool error = false;
if (state_ != kConnected)
{
LOG_WARN << "state = " << state_ << ", give up writing";
return;
}
// if no thing in output queue, try writing directly
if (!channel_->isWriting() && outputBuffer_.readableBytes() == 0)
{
Expand All @@ -156,12 +162,16 @@ void TcpConnection::sendInLoop(const void* data, size_t len)
if (errno != EWOULDBLOCK)
{
LOG_SYSERR << "TcpConnection::sendInLoop";
if (errno == EPIPE) // FIXME: any others?
{
error = true;
}
}
}
}

assert(remaining <= len);
if (remaining > 0)
if (!error && remaining > 0)
{
LOG_TRACE << "I am going to write more data";
size_t oldLen = outputBuffer_.readableBytes();
Expand Down Expand Up @@ -289,14 +299,15 @@ void TcpConnection::handleWrite()
}
else
{
LOG_TRACE << "Connection is down, no more writing";
LOG_TRACE << "Connection fd = " << channel_->fd()
<< " is down, no more writing";
}
}

void TcpConnection::handleClose()
{
loop_->assertInLoopThread();
LOG_TRACE << "TcpConnection::handleClose state = " << state_;
LOG_TRACE << "fd = " << channel_->fd() << " state = " << state_;
assert(state_ == kConnected || state_ == kDisconnecting);
// we don't close fd, leave it to dtor, so we can find leaks easily.
setState(kDisconnected);
Expand Down

2 comments on commit dd26871

@lilijreey
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

陈兄你好: 请问 ProcessInfo 文件的主要功能是什么? /proc/self 中主要都是什么信息? 谢谢

@chenshuo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send question to my email, do not comment in irrelevant commits.

Please sign in to comment.