close()SSL套接字直接在receive()之后:unsafe?

时间:2018-05-10 07:44:43

标签: java sockets ssl tcp race-condition

在最后一次接收()之后直接关闭()套接字是明智/安全吗?

我在问,因为我检查了套接字发送端发生了什么,如果我使用的是Java和TLS,那么代码就是

/**
 * Write the data out, NOW.
 */
@Override
public synchronized void write(byte[] b, int off, int len)
        throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return;
    }

    // check if the Socket is invalid (error or closed)
    socket.checkWrite();

    // Delegate the writing to the underlying socket.
    try {
        socket.writeRecord(b, off, len);
        socket.checkWrite();
    } catch (Exception e) {
        // shutdown and rethrow (wrapped) exception as appropriate
        socket.handleException(e);
    }
}

来自:http://hg.openjdk.java.net/jdk/jdk/file/26ac622a4cab/src/java.base/share/classes/sun/security/ssl/AppOutputStream.java#l68

这意味着,在第二次调用socket.checkWrite();时,Java会在发送最后一块数据后检查套接字是否可写。然后竞赛是可能的,在接收端我收到最后一个块后关闭,但在发送方进行最后一次检查之前,我最终在发送端抛出SocketException

我在上面的段落中描述的内容似乎是https://issues.apache.org/jira/browse/AMQ-6956中实际发生的事情。第二次检查时的套接字处于状态cs_SENT_CLOSE = 5,我得到SocketException

相关:close() socket directly after send(): unsafe?

0 个答案:

没有答案