From 86c4544ef9af169d590048d499edfd3e255e16d6 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Sat, 13 May 2017 16:22:14 +0300
Subject: [PATCH] zephyr/modusocket: If there're no packets in recv_q, cancel
 waiter.

This solves a case when socker_read() has blocked on fifo, and then peer
closed event arrives.
---
 zephyr/modusocket.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c
index d40b4887c..daaa15748 100644
--- a/zephyr/modusocket.c
+++ b/zephyr/modusocket.c
@@ -153,6 +153,7 @@ static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, i
         struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q);
         if (last_pkt == NULL) {
             socket->state = STATE_PEER_CLOSED;
+            k_fifo_cancel_wait(&socket->recv_q);
             DEBUG_printf("Marked socket %p as peer-closed\n", socket);
         } else {
             // We abuse "buf_sent" flag to store EOF flag
@@ -378,6 +379,11 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
                 DEBUG_printf("TCP recv: no cur_pkt, getting\n");
                 struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
 
+                if (pkt == NULL) {
+                    DEBUG_printf("TCP recv: NULL return from fifo\n");
+                    continue;
+                }
+
                 DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt);
                 socket->cur_pkt = pkt;
             }
-- 
GitLab