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

Deadlock in Client.close() #93

Closed
georgeto opened this issue May 8, 2015 · 0 comments
Closed

Deadlock in Client.close() #93

georgeto opened this issue May 8, 2015 · 0 comments

Comments

@georgeto
Copy link
Contributor

georgeto commented May 8, 2015

This scenario occured during execution of the JsonTest, but I'm pretty sure it is also possible in a real enviroment.

To understand this problem, it is important to know, that Selector.selectNow() synchronizes on Selector, Selector.keys() and Selector.selectedKeys().

public int selectNow() throws IOException {
    return lockAndDoSelect(0);
}

private int lockAndDoSelect(long timeout) throws IOException {
    synchronized (this) {
        if (!isOpen())
            throw new ClosedSelectorException();
        synchronized (publicKeys) {
            synchronized (publicSelectedKeys) {
                return doSelect(timeout);
            }
        }
    }
}

[Thread 1]

  1. Client.update() locks selector
  2. Client's received handler sends a tcp message with connection.sendTCP()

[Thread 2]
3. Client.stop() is called, and calls Client.close()
4. Client.close() closes the tcp connection
5. Client.close() grabs updateLock
6. Client.close() calls selector.selectNow(), and waits for [Thread 1] to unlock selector

[Thread 1]
6. Cause tcp connection is closed, Client.sendTCP() throws an IOException and calls Client.close()
7. Client.close() waits for [Thread 2] to unlock updateLock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant