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 @@ -1624,21 +1624,42 @@ void AsyncServer::end() {
1624
1624
1625
1625
// runs on LwIP thread
1626
1626
int8_t AsyncServer::_accept (tcp_pcb *pcb, int8_t err) {
1627
- // ets_printf("+A: 0x%08x\n", pcb);
1628
- if (_connect_cb) {
1629
- AsyncClient *c = new (std::nothrow) AsyncClient (pcb);
1630
- if (c) {
1631
- c->setNoDelay (_noDelay);
1632
- const int8_t err = _tcp_accept (this , c);
1633
- if (err != ERR_OK) {
1627
+ if (pcb) {
1628
+ if (_connect_cb) {
1629
+ AsyncClient *c = new (std::nothrow) AsyncClient (pcb);
1630
+ if (c && c->pcb ()) {
1631
+ c->setNoDelay (_noDelay);
1632
+ if (_tcp_accept (this , c) == ERR_OK) {
1633
+ return ERR_OK; // success
1634
+ }
1635
+ }
1636
+ if (c->pcb ()) {
1637
+ // Couldn't allocate accept event
1638
+ // 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
1639
+ c->_pcb = nullptr ;
1634
1640
tcp_abort (pcb);
1641
+ log_e (" _accept failed: couldn't allocate client" );
1642
+ return ERR_ABRT;
1643
+ }
1644
+ if (c) {
1645
+ // Couldn't complete setup
1646
+ // pcb has already been aborted
1635
1647
delete c;
1648
+ pcb = nullptr ;
1649
+ log_e (" _accept failed: couldn't complete setup" );
1650
+ return ERR_ABRT;
1636
1651
}
1637
- return err;
1638
1652
}
1653
+ if (pcb) {
1654
+ if (tcp_close (pcb) != ERR_OK) {
1655
+ tcp_abort (pcb);
1656
+ }
1657
+ }
1658
+ } else {
1659
+ log_e (" _accept failed: pcb is NULL" );
1660
+ return ERR_ABRT;
1639
1661
}
1640
- tcp_abort (pcb);
1641
- log_d (" _accept failed" );
1662
+ log_e (" _accept failed" );
1642
1663
return ERR_OK;
1643
1664
}
1644
1665
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