Skip to content

Commit 512035d

Browse files
authored
Merge pull request me-no-dev#67 from matt123p/fix-crash-on-fin-v2
Fix crash on fin v3
2 parents b96c4fe + f6f5cb6 commit 512035d

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/AsyncTCP.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,20 @@ typedef struct {
7878

7979
static xQueueHandle _async_queue;
8080
static TaskHandle_t _async_service_task_handle = NULL;
81+
82+
83+
SemaphoreHandle_t _slots_lock;
8184
const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP;
82-
static int _closed_index = 0;
8385
static int _closed_slots[_number_of_closed_slots];
86+
static int _closed_index = []() {
87+
_slots_lock = xSemaphoreCreateBinary();
88+
xSemaphoreGive(_slots_lock);
89+
for (int i = 0; i < _number_of_closed_slots; ++ i) {
90+
_closed_slots[i] = 1;
91+
}
92+
return 1;
93+
}();
94+
8495

8596
static inline bool _init_async_event_queue(){
8697
if(!_async_queue){
@@ -554,22 +565,16 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
554565
_pcb = pcb;
555566
_closed_slot = -1;
556567
if(_pcb){
557-
_closed_slot = 0;
558-
if (_closed_index == 0) {
559-
_closed_index = 1;
560-
for (int i = 0; i < _number_of_closed_slots; ++ i) {
561-
_closed_slots[i] = 1;
562-
}
563-
} else {
564-
int closed_slot_min_index = _closed_slots[0];
565-
for (int i = 0; i < _number_of_closed_slots; ++ i) {
566-
if (_closed_slots[i] <= closed_slot_min_index && _closed_slots[i] != 0) {
567-
closed_slot_min_index = _closed_slots[i];
568-
_closed_slot = i;
569-
}
568+
xSemaphoreTake(_slots_lock, portMAX_DELAY);
569+
int closed_slot_min_index = 0;
570+
for (int i = 0; i < _number_of_closed_slots; ++ i) {
571+
if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) {
572+
closed_slot_min_index = _closed_slots[i];
573+
_closed_slot = i;
570574
}
571575
}
572576
_closed_slots[_closed_slot] = 0;
577+
xSemaphoreGive(_slots_lock);
573578

574579
_rx_last_packet = millis();
575580
tcp_arg(_pcb, this);

0 commit comments

Comments
 (0)