<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/52543/udp-socket-becomes-invalid-during-long-psm-sleep</link><description>Hi, 
 I&amp;#39;ve recently switched some prototype code from using MQTT to a custom UDP-based protocol. What I noticed is that if I have the unit sleep for longer periods of time, when it wakes up, the UDP socket behaves oddly. Generally the behavior is that</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 30 Sep 2019 15:18:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/52543/udp-socket-becomes-invalid-during-long-psm-sleep" /><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/212660?ContentTypeID=1</link><pubDate>Mon, 30 Sep 2019 15:18:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:752a55df-91b0-4175-aa73-e440bf6c2ad5</guid><dc:creator>Eric Gross</dc:creator><description>&lt;p&gt;Thanks, I think this was my next item to refactor. I had a bunch of different tasks at different rates and the workqueue submission with a delay made it easy to multiplex them in a simple manner compared to managing it myself with tracking time in a main() loop like some of the examples do. I&amp;#39;ll switch to a dedicated workqueue though.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/212656?ContentTypeID=1</link><pubDate>Mon, 30 Sep 2019 15:13:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:920a5d0e-a961-422f-ad1c-71e6a2bab663</guid><dc:creator>jbrzozoski</dc:creator><description>[quote userid="76621" url="~/f/nordic-q-a/52543/udp-socket-becomes-invalid-during-long-psm-sleep/212133"]the rather vague warning from the Zephyr documentation about doing any blocking calls in the system workqueue[/quote]
&lt;p&gt;Short version: many different parts of Zephyr make use of the system workqueue.&amp;nbsp; It&amp;#39;s effectively a single thread that different drivers/libraries can make use of when they need to do work in a&amp;nbsp;lower priority context than their normal thread or interrupt level.&amp;nbsp; The issue is that if any of the workqueue handler functions block, all of the other requests to have work done in the system workqueue are blocked behind it (because it&amp;#39;s just one thread).&lt;/p&gt;
&lt;p&gt;So, for example, if your workqueue function blocks on a socket function, it could have unexpected results causing other drivers to stop working until your function unblocks.&lt;/p&gt;
&lt;p&gt;If you really like the workqueue API but know your handlers might need to block sometimes, you can declare additional workqueue threads and submit your blocking work to one of those to ensure the system workqueue remains unblocked.&amp;nbsp; The system workqueue is just a sort of default workqueue thread that is assumed to be always present.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/212344?ContentTypeID=1</link><pubDate>Fri, 27 Sep 2019 15:12:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c02d21d8-b191-4469-88e7-5680fa2c90f7</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>[quote user="Eric Gross"]Regarding the blocking calls in the k_work callback, I wasn&amp;#39;t concerned about being in ISR context, but rather the rather vague warning from the Zephyr documentation about doing any blocking calls in the system workqueue. I don&amp;#39;t have a separate workqueue currently defined.[/quote]
&lt;p&gt;&amp;nbsp;That depends on what a blocking call is. A while(1) will essentially block other threads, with equal or lower priority, but a recv() (or other&amp;nbsp;&amp;quot;infinite wait&amp;quot; socket calls) will yield in that sense, due to the bsd_os.c::bsd_os_timedwait() implementation which handles the timing on the sockets.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Eric Gross"]I&amp;#39;ll be out-of-the-office for a bit&amp;nbsp;before I can get back to trying your suggestions, so I&amp;#39;ll give you an update once I have a chance.[/quote]
&lt;p&gt;&amp;nbsp;Ok, just update me when you pick up the work again. Hope you have a nice weekend!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/212133?ContentTypeID=1</link><pubDate>Thu, 26 Sep 2019 19:41:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1c938338-98ed-4dd9-a19d-6d712a92134d</guid><dc:creator>Eric Gross</dc:creator><description>&lt;p&gt;Thanks again for all the great assistance! I have a lot of things to follow up with on my side based on your suggestions. I wasn&amp;#39;t aware of the&amp;nbsp;&lt;span&gt;CONFIG_USE_SEGGER_RTT option, but that&amp;#39;s pretty neat.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Regarding the blocking calls in the k_work callback, I wasn&amp;#39;t concerned about being in ISR context, but rather the rather vague warning from the Zephyr documentation about doing any blocking calls in the system workqueue. I don&amp;#39;t have a separate workqueue currently defined.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be out-of-the-office for a bit&amp;nbsp;before I can get back to trying your suggestions, so I&amp;#39;ll give you an update once I have a chance.&lt;/p&gt;
&lt;p&gt;Eric&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/212054?ContentTypeID=1</link><pubDate>Thu, 26 Sep 2019 12:25:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9eba13cd-6cbc-4178-84d5-63ba05e75fa8</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Eric,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Eric Gross"]Currently the only UART we have on a header on our PCB is UART0 for serial console.[/quote]
&lt;p&gt;&amp;nbsp;As a quick test, you can rename the uart0 to uart1 (and vice-versa) in the dts file, so that your default output will be routed to another set of pins, while the current routed uart gpios will run on &amp;quot;uart1&amp;quot;:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/boards/arm/nrf9160_pca10090/nrf9160_pca10090_common.dts#L83"&gt;https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/boards/arm/nrf9160_pca10090/nrf9160_pca10090_common.dts#L83&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Remember to uncheck the &amp;quot;auto device/filter port&amp;quot; option, so that all COMs are visible:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-3f96a99b0b234f4f897ac81e40b81812/pastedimage1569497643083v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The downside of this is that your print outs will not work, but you can use RTT for that by adding these to your prj.conf:&lt;/p&gt;
&lt;p&gt;CONFIG_USE_SEGGER_RTT=y&lt;br /&gt;CONFIG_LOG=y&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Eric Gross"]Thanks so much for your assistance with this. In your setup, did you also have PSM mode on the radio enabled?&amp;nbsp;[/quote]
&lt;p&gt;&amp;nbsp;Ran it for around ~18 hours, with PSM set to 120 minutes, and sending around every 120 minutes as well. The socket wasn&amp;#39;t lost.&lt;/p&gt;
&lt;p&gt;Based in this sample:&amp;nbsp;&lt;a href="https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/udp_with_psm"&gt;https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/udp_with_psm&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Where I needed to modify several things to change the PSM interval, as well as make bind/connect run only once + the socket_fd as a global.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Eric Gross"]Note that I do have some [possibly unrelated] questions about whether I should be doing these blocking calls from within a k_work callback.[/quote]
&lt;p&gt;&amp;nbsp;That should be fine, as long as you&amp;#39;re not calling socket calls from interrupts (uart callback, k_timer instance (but not work queues!), etc are interrupt context). If you&amp;#39;re unsure of which functions are interrupts, you can call k_is_in_isr() to check.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
[quote user="Eric Gross"]Open new socket and call send(), which returns errno 114.[/quote]
&lt;p&gt;&amp;nbsp;NRF_EALREADY&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 114 //&amp;lt; Operation already in progress&lt;/p&gt;
&lt;p&gt;It seems the socket is in a limbo state if you still get this errno back even after creating a new socket_fd. Could you try isolating the process of sending every 90 minutes, and see if the issue still occurs then?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/211907?ContentTypeID=1</link><pubDate>Wed, 25 Sep 2019 18:00:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7bd3983c-9b38-45cb-b73a-fded2bf601c0</guid><dc:creator>Eric Gross</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;H&amp;aring;kon,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks so much for your assistance with this. In your setup, did you also have PSM mode on the radio enabled?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Firewalls should not be an issue due to the network setup I am using (no firewalls), but also I would not expect them to affect the socket errors I am seeing, as they would simply cause packets to be dropped.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Regarding RAM corruption of the socket_fd, that is an interesting angle. I was thinking that it should be unrelated to the time interval used, but that is not quite true, as my application buffers data in the background at a different rate than the transmission cycle. I suppose it is possible that somehow more data being buffered could be causing corruption somehow, but still seems unlikely with the way I have things structured.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I switched to a new pattern that still shows the issue:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-PSM mode set&amp;nbsp; to 190 min and k_work queued for 90 min; CPU goes to sleep&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-After 90 min, CPU wakes up and runs k_work callback, which opens new UDP socket on ephemeral_port, call sendto() to remote IP addr, which succeeds, though server doesn&amp;#39;t get packet)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-Wait on poll() for response from server. This returns&amp;nbsp;POLLHUP instead of timing out.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-Sleep for 1000ms. Close socket, loop back to beginning for next attempt&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-Open new socket and call send(), which returns errno 114. Wait 1000ms, close socket, and loop back for next attempt&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;-On 3rd attempt, send succeeds, poll succeeds, and recv() succeeds&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note that I do have some [possibly unrelated] questions about whether I should be doing these blocking calls from within a k_work callback.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regarding the modem trace, this application is currently not running on a DK board but rather our own board.&amp;nbsp;The instructions you pointed me to seem to imply it uses the nrf52&amp;#39;s UART to collect the data from the 9160. I&amp;#39;d have to figure out how to make that work with our setup. Currently the only UART we have on a header on our PCB is UART0 for serial console.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/211901?ContentTypeID=1</link><pubDate>Wed, 25 Sep 2019 17:17:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b45a694f-7d4d-4da3-bf65-c76dcef23596</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you, it seems that you are using quite a new firmware version, and the latest modem.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m setting up a test bench on this matter, where I kept the socket open and initiated a UDP send/recv every 120 minutes. This worked at my end, using similar setup as you.&lt;/p&gt;
&lt;p&gt;However; I am connected to a different vendor, so I am unable to check if there&amp;#39;s any in the network you&amp;#39;re connected to that is blocking this for you. Some firewalls detect UDP send/recv cycles, and times these dynamically, but this is just one of the things that might be causing this behavior on your side. I assume that there&amp;#39;s other routines running in your system, could RAM corruption of the socket_fd be a root cause, for instance?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you try to do a modem trace that shows this issue?&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s a guide on how to do this:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/how-to-get-modem-trace-using-trace-collector-in-nrf-connect"&gt;https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/how-to-get-modem-trace-using-trace-collector-in-nrf-connect&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For the firmware side; Its essentially&amp;nbsp;adding &amp;quot;CONFIG_BSD_LIBRARY_TRACE_ENABLED=y&amp;quot; to your prj.conf&amp;nbsp;and sending AT command &amp;quot;AT%XMODEMTRACE=1,2&amp;quot; at boot up.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note: The modem trace should be several kilobytes at the start (and continue to grow, especially in the beginning), if the above process has worked.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/211844?ContentTypeID=1</link><pubDate>Wed, 25 Sep 2019 13:14:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac808256-3924-44ec-bd26-c0a36d1891f5</guid><dc:creator>Eric Gross</dc:creator><description>&lt;p&gt;mfw_nrf9160_1.0.1&lt;/p&gt;
&lt;p&gt;v1.14.99-ncs3-snapshot2-1266-g8711cfd5d348&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UDP socket becomes invalid during long PSM sleep</title><link>https://devzone.nordicsemi.com/thread/211700?ContentTypeID=1</link><pubDate>Wed, 25 Sep 2019 07:53:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:200a14b9-a614-47af-9fd3-3b08d4e3fd74</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q1: What modem fw are you running on?&lt;/p&gt;
&lt;p&gt;Q2: Which version of ncs are you running?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>