<?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>nRF9151: fcntl() errno 9 EBADF</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/125951/nrf9151-fcntl-errno-9-ebadf</link><description>Hi, 
 
 When we open a TCP socket in our nRF9151 application, we set it to non-blocking. 
 The flow: 
 
 Sometimes, seemingly at random, nrf_fcntl() returns -1 and errno == 9 (EBADF) . 
 We also check the return values of socket() and setsockopt() and</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 09 Dec 2025 07:40:11 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/125951/nrf9151-fcntl-errno-9-ebadf" /><item><title>RE: nRF9151: fcntl() errno 9 EBADF</title><link>https://devzone.nordicsemi.com/thread/556519?ContentTypeID=1</link><pubDate>Tue, 09 Dec 2025 07:40:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eb3e9559-464f-4160-ac57-b5624a18b43a</guid><dc:creator>Hakon</dc:creator><description>&lt;p&gt;Using fcntl() instead of nrf_fcntl() seems to work for me. This kinda makes sense since you are using the Zephyr socket API when creating the socket, but you are calling nrf_fcntl() which is the modem lib API. I think you should stick to one API when dealing with sockets.&lt;/p&gt;
&lt;p&gt;Edit; &lt;span style="text-decoration:line-through;"&gt;Never mind, it works with the Zephyr stack, but not with the offloaded sockets.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Edit2;&amp;nbsp;&lt;strong&gt;This&lt;/strong&gt; works for me;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;ncs_version.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/net/socket.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;

#include &amp;lt;nrf_socket.h&amp;gt;
#include &amp;lt;modem/nrf_modem_lib.h&amp;gt;
#include &amp;lt;modem/lte_lc.h&amp;gt;

int main(void) {
    int fd;
    int err;
    printk(&amp;quot;Starting fcntl test\n&amp;quot;);
    
    err = nrf_modem_lib_init();
    fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    /* fd &amp;gt;= 0 */

    int rai = RAI_ONGOING;
    err = setsockopt(fd, SOL_SOCKET, SO_RAI, &amp;amp;rai, sizeof(rai));
    /* err == 0 */

    err = fcntl(fd, NRF_F_SETFL, NRF_O_NONBLOCK);
    /* err == -1 &amp;amp;&amp;amp; errno == 9 */
    printk(&amp;quot;%d\n&amp;quot;, err);
    printk(&amp;quot;%d\n&amp;quot;, errno);
    return 0;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9151: fcntl() errno 9 EBADF</title><link>https://devzone.nordicsemi.com/thread/556328?ContentTypeID=1</link><pubDate>Fri, 05 Dec 2025 12:53:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:181326f4-4cc0-4c4d-b442-5d41fb40813e</guid><dc:creator>klaasg</dc:creator><description>&lt;p&gt;We use SDK v2.7.0 (working on moving up), modem firmware 2.0.2 on nRF9151.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s really this simple piece of code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
/* fd &amp;gt;= 0 */

int rai = RAI_ONGOING;
err = setsockopt(fd, SOL_SOCKET, SO_RAI, &amp;amp;rai, sizeof(rai));
/* err == 0 */

nrf_fcntl(fd, NRF_F_SETFL, NRF_O_NONBLOCK);
/* err == -1 &amp;amp;&amp;amp; errno == 9 */&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;While Registered and RRC Connected on an NB-IoT network (example 20620), sometimes nrf_fcntl returns this.&lt;/p&gt;
&lt;p&gt;The response is immediate (&amp;lt;1ms).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;In what case does fcntl set errno to EBADF?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9151: fcntl() errno 9 EBADF</title><link>https://devzone.nordicsemi.com/thread/556241?ContentTypeID=1</link><pubDate>Thu, 04 Dec 2025 13:18:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:395248a4-4c1c-4563-b3b2-2c07afe7e6ca</guid><dc:creator>Hakon</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;can you provide more information? What SDK, modem firmware, and hardware are you using? Any logs?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>