@@ -167,6 +167,7 @@ static inline bool _get_async_event(lwip_tcp_event_packet_t **e) {
167
167
*/
168
168
lwip_tcp_event_packet_t *next_pkt = NULL ;
169
169
while (xQueuePeek (_async_queue, &next_pkt, 0 ) == pdPASS) {
170
+ // if the next event that will come is a poll event for the same connection, we can discard it and continue
170
171
if (next_pkt->arg == (*e)->arg && next_pkt->event == LWIP_TCP_POLL) {
171
172
if (xQueueReceive (_async_queue, &next_pkt, 0 ) == pdPASS) {
172
173
free (next_pkt);
@@ -176,7 +177,7 @@ static inline bool _get_async_event(lwip_tcp_event_packet_t **e) {
176
177
}
177
178
}
178
179
179
- // quit while loop if next event can't be discarded
180
+ // quit while loop if next incoming event can't be discarded (not a poll event)
180
181
break ;
181
182
}
182
183
@@ -193,9 +194,9 @@ static inline bool _get_async_event(lwip_tcp_event_packet_t **e) {
193
194
free (*e);
194
195
*e = NULL ;
195
196
log_d (" discarding poll due to queue congestion" );
196
- continue ; // Retry
197
+ continue ; // continue main loop to dequeue next event which we know is not a poll event
197
198
}
198
- return true ;
199
+ return true ; // queue not nearly full, caller can process the poll event
199
200
}
200
201
return false ;
201
202
}
@@ -355,6 +356,7 @@ static int8_t _tcp_clear_events(void *arg) {
355
356
e->arg = arg;
356
357
if (!_prepend_async_event (&e)) {
357
358
free ((void *)(e));
359
+ return ERR_TIMEOUT;
358
360
}
359
361
return ERR_OK;
360
362
}
@@ -372,6 +374,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
372
374
e->connected .err = err;
373
375
if (!_prepend_async_event (&e)) {
374
376
free ((void *)(e));
377
+ return ERR_TIMEOUT;
375
378
}
376
379
return ERR_OK;
377
380
}
@@ -396,6 +399,7 @@ static int8_t _tcp_poll(void *arg, struct tcp_pcb *pcb) {
396
399
// poll events are not critical 'cause those are repetitive, so we may not wait the queue in any case
397
400
if (!_send_async_event (&e, 0 )) {
398
401
free ((void *)(e));
402
+ return ERR_TIMEOUT;
399
403
}
400
404
return ERR_OK;
401
405
}
@@ -423,6 +427,7 @@ static int8_t _tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, int8_t
423
427
}
424
428
if (!_send_async_event (&e)) {
425
429
free ((void *)(e));
430
+ return ERR_TIMEOUT;
426
431
}
427
432
return ERR_OK;
428
433
}
@@ -440,6 +445,7 @@ static int8_t _tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
440
445
e->sent .len = len;
441
446
if (!_send_async_event (&e)) {
442
447
free ((void *)(e));
448
+ return ERR_TIMEOUT;
443
449
}
444
450
return ERR_OK;
445
451
}
@@ -491,6 +497,7 @@ static int8_t _tcp_accept(void *arg, AsyncClient *client) {
491
497
e->accept .client = client;
492
498
if (!_prepend_async_event (&e)) {
493
499
free ((void *)(e));
500
+ return ERR_TIMEOUT;
494
501
}
495
502
return ERR_OK;
496
503
}
@@ -1614,7 +1621,11 @@ int8_t AsyncServer::_accept(tcp_pcb *pcb, int8_t err) {
1614
1621
AsyncClient *c = new AsyncClient (pcb);
1615
1622
if (c) {
1616
1623
c->setNoDelay (_noDelay);
1617
- return _tcp_accept (this , c);
1624
+ const int8_t err = _tcp_accept (this , c);
1625
+ if (err != ERR_OK) {
1626
+ delete c;
1627
+ }
1628
+ return err;
1618
1629
}
1619
1630
}
1620
1631
if (tcp_close (pcb) != ERR_OK) {
0 commit comments