场景
Thrift框架采用了异常处理机制,当客户端异常断开连接,服务端这个时候尝试发送数据给客户端,Thrift库会抛出异常,导致进程中断。这种情况是非常正常的,服务器端应该捕获异常的发生,但是不应该异常退出。所以应该当前发送数据失败,直接返回
修改代码如下:
uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
if (socket_ == -1) {
return -1;
throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
}
uint32_t sent = 0;
int flags = 0;
#ifdef MSG_NOSIGNAL
// Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
// check for the EPIPE return condition and close the socket in that case
flags |= MSG_NOSIGNAL;
#endif // ifdef MSG_NOSIGNAL
int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags);
++g_socket_syscalls;
if (b < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
return 0;
}
// Fail on a send error
int errno_copy = errno;
GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
//修改的第一个地方,直接返回-1,不抛出异常
close();
return -1;
//throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
}
//修改的第二个地方,直接返回-1,不抛出异常
close();
return -1;
throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
}
// Fail on blocked send
if (b == 0) {
throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
}
return b;
}
如下是新的尝试,尝试在最外层进行异常的捕获
void TSocket::write(const uint8_t* buf, uint32_t len) {
uint32_t sent = 0;
while (sent < len) {
//尝试通过try捕获write_partial函数抛出的异常,但是在测试结果中并没有什么用,异常还是中断程序
try
{
uint32_t b = write_partial(buf + sent, len - sent);
if (b == 0) {
// This should only happen if the timeout set with SO_SNDTIMEO expired.
// Raise an exception.
throw TTransportException(TTransportException::TIMED_OUT,
"send timeout expired");
}
sent += b;
}
catch (apache::thrift::transport::TTransportException* e)
{
return;
}
}
}
分析一下服务器发送数据的函数
void TSocket::write(const uint8_t* buf, uint32_t len) {
uint32_t sent = 0;
while (sent < len) {
uint32_t b = write_partial(buf + sent, len - sent);
if (b == 0) {
// This should only happen if the timeout set with SO_SNDTIMEO expired.
// Raise an exception.
throw TTransportException(TTransportException::TIMED_OUT,
"send timeout expired");
}
sent += b;
}
}
但b==0抛出异常,代表当前发送超时。while循环是为了循环发送,因为一次不一定发送完用户数据,毕竟MTU的限制。注意sent是一个无符号整型,当b返回-1的时候,sent==0-1意味着将达到32位整数最大值,大于len,从而直接退出循环。因为套接字已经中断,所以发送失败,在调用write_partial函数的时候,返回b ==-1,导致退出循环,从而避免了抛出异常,因此返回-1,是非常合理的值
本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/2071771,如需转载请自行联系原作者