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): deadlock in lwIP UDP handler's Close function #30

Merged
merged 5 commits into from
Jul 27, 2023

Conversation

jyyi1
Copy link
Contributor

@jyyi1 jyyi1 commented Jul 21, 2023

This PR fixes a deadlock bug when a careless PacketRequestSender tries to Close the corresponding PacketResponseReceiver multiple times, here is the buggy flow chart:

user calls PacketResponseReceiver.Close()
                  ↓
     udpConnResponseWriter.Close()  <----------------------------------------(1)
                  ↓                                                           |
     udpHandler.closeSession(conn)                                            |
                  ↓                                                           |
              h.mu.Lock()                                                     |
                  ↓                                                           |
        h.senders[laddr].Close() → PacketRequestSender.Close() → PacketResponseReceiver.Close()

Note the recursive call path (1): h.senders[laddr].Close() will block because h.mu has already be held, and it won't Unlock() until h.senders[laddr].Close() returns, this causes deadlock!

This fix simply added a closed indicator in udpConnResponseWriter to make sure only one closeSession will be called:

  user calls PacketResponseReceiver.Close()
                  ↓
     udpConnResponseWriter.Close()
                  ↓
r.closed.CompareAndSwap(false, true) == true
                  ↓
     udpHandler.closeSession(conn)                                            x
                  ↓                                                           |
              h.mu.Lock()                                r.closed.CompareAndSwap(false, true) == false
                  ↓                                                           |
        h.senders[laddr].Close() → PacketRequestSender.Close() → PacketResponseReceiver.Close()
                  ↓
             h.mu.UnLock()

@jyyi1 jyyi1 marked this pull request as ready for review July 25, 2023 21:02
@jyyi1 jyyi1 requested a review from fortuna July 25, 2023 21:21
@jyyi1 jyyi1 merged commit a71afc8 into main Jul 27, 2023
4 checks passed
@jyyi1 jyyi1 deleted the junyi/fix-inf-close-loop-for-packet-proxy branch July 27, 2023 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants