File tree 2 files changed +33
-10
lines changed
2 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -1616,21 +1616,42 @@ void AsyncServer::end() {
1616
1616
1617
1617
// runs on LwIP thread
1618
1618
int8_t AsyncServer::_accept (tcp_pcb *pcb, int8_t err) {
1619
- // ets_printf("+A: 0x%08x\n", pcb);
1620
- if (_connect_cb) {
1621
- AsyncClient *c = new (std::nothrow) AsyncClient (pcb);
1622
- if (c) {
1623
- c->setNoDelay (_noDelay);
1624
- const int8_t err = _tcp_accept (this , c);
1625
- if (err != ERR_OK) {
1619
+ if (pcb) {
1620
+ if (_connect_cb) {
1621
+ AsyncClient *c = new (std::nothrow) AsyncClient (pcb);
1622
+ if (c && c->pcb ()) {
1623
+ c->setNoDelay (_noDelay);
1624
+ if (_tcp_accept (this , c) == ERR_OK) {
1625
+ return ERR_OK; // success
1626
+ }
1627
+ }
1628
+ if (c->pcb ()) {
1629
+ // Couldn't allocate accept event
1630
+ // We can't let the client object call in to close, as we're on the LWIP thread; it could deadlock trying to RPC to itself
1631
+ c->_pcb = nullptr ;
1626
1632
tcp_abort (pcb);
1633
+ log_e (" _accept failed: couldn't allocate client" );
1634
+ return ERR_ABRT;
1635
+ }
1636
+ if (c) {
1637
+ // Couldn't complete setup
1638
+ // pcb has already been aborted
1627
1639
delete c;
1640
+ pcb = nullptr ;
1641
+ log_e (" _accept failed: couldn't complete setup" );
1642
+ return ERR_ABRT;
1628
1643
}
1629
- return err;
1630
1644
}
1645
+ if (pcb) {
1646
+ if (tcp_close (pcb) != ERR_OK) {
1647
+ tcp_abort (pcb);
1648
+ }
1649
+ }
1650
+ } else {
1651
+ log_e (" _accept failed: pcb is NULL" );
1652
+ return ERR_ABRT;
1631
1653
}
1632
- tcp_abort (pcb);
1633
- log_d (" _accept failed" );
1654
+ log_e (" _accept failed" );
1634
1655
return ERR_OK;
1635
1656
}
1636
1657
Original file line number Diff line number Diff line change @@ -246,6 +246,8 @@ class AsyncClient {
246
246
}
247
247
248
248
protected:
249
+ friend class AsyncServer ;
250
+
249
251
bool _connect (ip_addr_t addr, uint16_t port);
250
252
251
253
tcp_pcb *_pcb;
You can’t perform that action at this time.
0 commit comments