<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Murat Toğunçhan Düzgün on Medium]]></title>
        <description><![CDATA[Stories by Murat Toğunçhan Düzgün on Medium]]></description>
        <link>https://medium.com/@togunchan?source=rss-767f3633b4ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*38TDz7FR2MGbC3RftNwtpw.jpeg</url>
            <title>Stories by Murat Toğunçhan Düzgün on Medium</title>
            <link>https://medium.com/@togunchan?source=rss-767f3633b4ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 01:54:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@togunchan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AI Challenged a 27-Year-Old Hypothesis. Can It Reason?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@togunchan/ai-was-supposed-to-predict-tokens-then-it-challenged-27-years-of-mathematics-679899118e97?source=rss-767f3633b4ad------2"><img src="https://cdn-images-1.medium.com/max/1672/1*n1fB17oeDyBIIyz6d9UbjA.png" width="1672"></a></p><p class="medium-feed-snippet">What OpenAI&#x2019;s proposed challenge to the Exponential Time Hypothesis reveals about mathematical proofs, verification, and AI reasoning.</p><p class="medium-feed-link"><a href="https://medium.com/@togunchan/ai-was-supposed-to-predict-tokens-then-it-challenged-27-years-of-mathematics-679899118e97?source=rss-767f3633b4ad------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@togunchan/ai-was-supposed-to-predict-tokens-then-it-challenged-27-years-of-mathematics-679899118e97?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/679899118e97</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sun, 01 Nov 2026 18:04:47 GMT</pubDate>
            <atom:updated>2026-11-01T18:07:06.728Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Is Happiness the Silence of Desire or the Struggle to Become?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@togunchan/is-happiness-the-silence-of-desire-or-the-struggle-to-become-69b2eba8ecb5?source=rss-767f3633b4ad------2"><img src="https://cdn-images-1.medium.com/max/1672/1*GZnAhTjr_PIRivKboiSRrQ.png" width="1672"></a></p><p class="medium-feed-snippet">Schopenhauer, Nietzsche, and the question of which desires are worth pursuing</p><p class="medium-feed-link"><a href="https://medium.com/@togunchan/is-happiness-the-silence-of-desire-or-the-struggle-to-become-69b2eba8ecb5?source=rss-767f3633b4ad------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@togunchan/is-happiness-the-silence-of-desire-or-the-struggle-to-become-69b2eba8ecb5?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/69b2eba8ecb5</guid>
            <category><![CDATA[happiness]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[arthur-schopenhauer]]></category>
            <category><![CDATA[friedrich-nietzsche]]></category>
            <category><![CDATA[philosophy]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sat, 03 Oct 2026 19:22:59 GMT</pubDate>
            <atom:updated>2026-10-03T19:22:59.570Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Ownership Comes Before Smart Pointers]]></title>
            <link>https://levelup.gitconnected.com/ownership-comes-before-smart-pointers-540d4199ddcf?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/540d4199ddcf</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[smart-pointer]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sun, 27 Sep 2026 19:00:03 GMT</pubDate>
            <atom:updated>2026-09-28T14:12:37.030Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qr3270yKOL-mgsaqp_Ieog.png" /></figure><h3>Smart pointers can enforce an ownership model, but they cannot decide what that model should be.</h3><p>Consider a Session that depends on a Connection:</p><pre>class Session {<br>public:<br>    explicit Session(std::shared_ptr&lt;Connection&gt; connection)<br>        : connection_(std::move(connection)) {}<br><br>private:<br>    std::shared_ptr&lt;Connection&gt; connection_;<br>};</pre><p>At first glance, this looks like disciplined modern C++. There is no manual <strong>delete</strong>, the <strong>Connection</strong> cannot disappear while the <strong>Session</strong> still holds it, and destruction is handled automatically when the last <strong>shared_ptr</strong> goes away.</p><p>But the pointer type has already made a more important decision: a <strong>Session</strong> is now allowed to keep the <strong>Connection</strong> alive. That may be exactly what the architecture requires, but the fact that a session needs to access a connection does not establish that it should also participate in controlling its lifetime.</p><p>This distinction matters because smart pointers enforce specific lifetime mechanics after an ownership relationship has been chosen. A <strong>unique_ptr</strong> ties an object&#39;s lifetime to one owner and cannot be copied, so moving it with <strong>std::move</strong> makes a transfer of ownership visible in the code. A <strong>shared_ptr</strong> lets several owners participate in the same lifetime through a shared reference count, destroying the object when the last strong owner disappears. A <strong>weak_ptr</strong> refers to that shared lifetime without increasing the strong reference count, allowing access only after confirming that the object still exists. These mechanisms can enforce an ownership model, but they cannot determine which components should own an object in the first place.</p><p>That decision belongs to the architecture.</p><h3>Multiple users do not imply multiple owners</h3><p>Several components needing the same object is often where ownership becomes unclear. A common reaction is to use <strong>shared_ptr</strong>, because the object now appears to be “shared.” That reasoning combines two different properties: multiple components can use the same object, but they do not necessarily need independent authority to keep it alive.</p><p><a href="https://github.com/togunchan/EdgeNetSwitch">EdgeNetSwitch</a> encountered this distinction when UDP ingress expanded from one receiver to multiple configured endpoints. Every receiver needed lifecycle IDs, and those IDs had to remain unique across the runtime. The runtime therefore uses one <strong>LifecycleIdGenerator</strong>, and <a href="https://github.com/togunchan/EdgeNetSwitch/blob/2fea1bc72250abde6c051bd253013cd44467c64e/src/runtime/IngressManager.cpp"><strong>IngressManager</strong></a> receives it by reference:</p><pre>explicit IngressManager(MessagingBus &amp;bus, <br>                        LifecycleIdGenerator &amp;lifecycleGenerator,<br>                        EpollManager &amp;epollManager, <br>                        EpollEventLoop &amp;epollEventLoop,<br>                        FdRegistry &amp;fdRegistry);<br><br>LifecycleIdGenerator &amp;lifecycleGenerator_;</pre><p>When an ingress endpoint is created, the same generator is passed to its receiver:</p><pre>UdpIngressEndpoint endpoint;<br><br>endpoint.receiver = std::make_unique&lt;UdpReceiver&gt;(<br>    bus_, <br>    endpointConfig.switch_port, <br>    endpointConfig.listen.port, <br>    lifecycleGenerator_,<br>    &amp;fdRegistry_, <br>    IngressMode::NonBlocking);</pre><p>There may be several <strong>UdpReceiver</strong> instances, but there is no need for each receiver to become an owner of the generator. They need access to runtime-wide identity state; they do not need authority over its lifetime.</p><p>It is useful to separate two levels of terminology here. In C++, an <strong>object</strong> is a runtime entity with a lifetime: a <strong>UdpReceiver</strong>, a <strong>LifecycleIdGenerator</strong>, or a <strong>FileDescriptor</strong> instance. A <strong>component</strong>, as used in this article, is an architectural unit with a responsibility: for example, the ingress subsystem, <strong>IngressManager</strong>, or the transport layer. A component may create, contain, coordinate, or use several C++ objects. Ownership is enforced on object lifetimes, but the decision about where that ownership should belong is usually an architectural decision made at component boundaries.</p><p>This distinction also prevents a common shortcut in reasoning. Many objects may access the same object while only one component remains responsible for its lifetime. Conversely, two independent components may genuinely need authority to keep the same object alive. The number of references or consumers therefore tells us almost nothing about the correct ownership model.</p><p>The relevant question is not how many objects can reach another object. It is which component is responsible for that object’s lifetime, and whether any other component genuinely needs the authority to extend it.</p><h3>Ownership becomes useful when it has an architectural home</h3><p><strong>std::unique_ptr</strong> is usually introduced as the smart pointer that permits only one owner. That definition is correct, but the more useful architectural property is that it gives lifetime responsibility a specific location.</p><p>The ingress path in EdgeNetSwitch provides a concrete example. Each configured endpoint retains its receiver and readiness handler through <strong>unique_ptr</strong>:</p><pre>struct UdpIngressEndpoint<br>{<br>    std::unique_ptr&lt;UdpReceiver&gt; receiver;<br>    std::unique_ptr&lt;UdpReadyHandler&gt; handler;<br>};<br><br>std::vector&lt;UdpIngressEndpoint&gt; ingressEndpoints_;</pre><p><strong>IngressManager</strong> creates those objects:</p><pre>UdpIngressEndpoint endpoint;<br><br>endpoint.receiver = std::make_unique&lt;UdpReceiver&gt;(<br>    bus_, <br>    endpointConfig.switch_port, <br>    endpointConfig.listen.port, <br>    lifecycleGenerator_,<br>    &amp;fdRegistry_, <br>    IngressMode::NonBlocking);<br><br>endpoint.receiver-&gt;initializeSocket();<br><br>endpoint.handler = std::make_unique&lt;UdpReadyHandler&gt;(*endpoint.receiver);<br>Logger::debug(&quot;UDP fd = &quot; + std::to_string(endpoint.receiver-&gt;fd()));<br><br>epollManager_.add(endpoint.receiver-&gt;fd(), EPOLLIN);<br>epollLoop_.registerHandler(endpoint.receiver-&gt;fd(), endpoint.handler.get());<br><br>ingressEndpoints_.push_back(std::move(endpoint));</pre><p>The important part is what can be inferred from the code: ingress endpoint lifetime belongs to <strong>IngressManager</strong>.</p><p>The receiver may interact with the messaging bus, the epoll loop may dispatch readiness to its handler, and other runtime components may inspect ingress state. None of those relationships requires those components to become co-owners of the receiver.</p><p>This is where <strong>unique_ptr</strong> provides more than automatic deletion. It records an architectural decision that already exists: these objects belong to the ingress subsystem, and that subsystem controls when their lifetimes end.</p><p>If the same objects had instead been distributed as <strong>shared_ptr</strong> throughout the runtime, destruction would still be automatic, but the ownership boundary would become weaker. A component that was intended only to use or observe an endpoint could also extend its lifetime.</p><p>The code would be mechanically safe while expressing a different architecture.</p><h3>Ownership transfer should be visible</h3><p>The transport layer applies the same idea at another boundary. <strong>TransportManager</strong> accepts a backend as a <strong>unique_ptr</strong>:</p><pre>void registerBackend(std::uint32_t port_id, <br>                     std::unique_ptr&lt;PortBackend&gt; backend);</pre><p>Its backend collection retains the same ownership semantics:</p><pre>std::unordered_map&lt;std::uint32_t, <br>                   std::unique_ptr&lt;PortBackend&gt;&gt; backends_;</pre><p>Registration moves the backend into the manager:</p><pre>backends_[port_id] = std::move(backend);</pre><p>This is where “ownership transfer” becomes concrete. A <strong>unique_ptr</strong> cannot be copied, so both the caller and <strong>TransportManager</strong> cannot independently become owners of the same backend through ordinary copying. Passing the pointer with <strong>std::move</strong> transfers the managed pointer into the destination <strong>unique_ptr</strong>; the source no longer owns the backend.</p><p>Later, transmission uses that owned backend:</p><pre>auto result = it-&gt;second-&gt;transmit(packet);</pre><p>Before registration, the caller owns the backend. The registration API defines what happens next: accepting a <strong>unique_ptr&lt;PortBackend&gt; </strong>means that registering a backend transfers ownership to <strong>TransportManager</strong>. Storing that pointer in <strong>backends_</strong> then makes the manager responsible for keeping the backend alive for as long as it remains registered.</p><p>Code that later sends packets through <strong>TransportManager</strong> does not receive that ownership. It asks the manager to use a backend that the manager already owns.</p><p>The API boundary therefore does more than reveal an ownership decision. It establishes the ownership contract between the caller and <strong>TransportManager</strong> registration transfers the backend, and the manager retains it. <strong>unique_ptr</strong> makes that contract enforceable because the backend cannot also remain owned by the caller through an ordinary copy.</p><h3>Shared ownership changes the reasoning model</h3><p>EdgeNetSwitch also contains a case where multiple readers may need to keep the same published snapshot alive independently of the publisher.</p><p><strong>SnapshotPublisher</strong> stores the currently published runtime status as a <strong>shared_ptr</strong> and returns the same ownership form to readers:</p><pre>class SnapshotPublisher final<br>{<br>public:<br>    SnapshotPublisher() = default;<br>    std::uint64_t publish(RuntimeStatus &amp;status);<br>    std::shared_ptr&lt;const RuntimeStatus&gt; load() const;<br>    std::uint64_t version() const noexcept;<br><br>private:<br>    std::atomic&lt;std::uint64_t&gt; version_{0};<br>    std::shared_ptr&lt;const RuntimeStatus&gt; snapshot_;<br>};</pre><p>The return type of <strong>load()</strong> changes the lifetime relationship. When a caller receives the result:</p><pre>auto snap = g_snapshotPublisher.load();<br>if (snap)<br>{<br>    Logger::debug(<br>        &quot;debug_reader: version=&quot; + std::to_string(snap-&gt;snapshot_version) +<br>        &quot; tick=&quot; + std::to_string(snap-&gt;metrics.tick_count));<br>}</pre><p><strong>snapshot</strong> is itself another <strong>shared_ptr</strong> that shares ownership of the same <strong>RuntimeStatus</strong> object. The <strong>RuntimeStatus</strong> is not copied. Instead, the publisher’s <strong>shared_ptr</strong> and the caller’s <strong>shared_ptr</strong> participate in the same ownership state.</p><pre>publisher.shopshot_<br>snap</pre><p>As long as either strong owner remains, that RuntimeStatus stays alive.</p><p>This becomes important when the publisher moves on to another snapshot. Suppose a reader still holds the old one:</p><pre>auto oldSnapshot = publisher.load();<br><br>// The publisher later publishes newer state.<br>publisher.publish(newStatus);<br><br>// oldSnapshot can still refer to the previous RuntimeStatus.<br>readMetrics(*oldSnapshot);</pre><p>The publisher no longer needs to keep its own <strong>shared_ptr</strong> pointing to that previous snapshot. <strong>oldSnapshot</strong> is enough to keep the previous <strong>RuntimeStatus</strong> alive. The old object is destroyed only after the last <strong>shared_ptr</strong> that owns it is released or destroyed.</p><p>That behavior is different from returning a reference:</p><pre>const RuntimeStatus&amp; load() const;</pre><p>A reference would give the caller access to the object but no lifetime control. If the publisher replaced or destroyed the referenced object, the caller would need some other guarantee that its reference remained valid.</p><p>Returning <strong>std::shared_ptr&lt;const RuntimeStatus&gt; </strong>gives the caller a stronger guarantee: once it has obtained a snapshot, it can keep that exact snapshot alive for as long as it holds the returned <strong>shared_ptr</strong>.</p><p>This is a case where shared ownership follows directly from the requirement. The publisher needs to replace the current snapshot over time, while readers may still need earlier snapshots. Neither side can determine the lifetime of an old snapshot alone. Its lifetime ends only when the publisher and every reader that still owns that snapshot have released it.</p><p>The <strong>const</strong> in the return type adds another useful constraint:</p><pre>std::shared_ptr&lt;const RuntimeStatus&gt;</pre><p>Readers share ownership of the object’s lifetime, but they do not receive mutable access through this pointer. A reader can therefore keep a published snapshot alive without being allowed to modify it.</p><p>This separates two concerns that are easy to conflate: multiple components can share <strong>lifetime ownership</strong> without sharing <strong>mutation authority</strong>.</p><p>The example also shows why simply counting users is not enough to justify <strong>shared_ptr</strong>. The earlier <strong>LifecycleIdGenerator</strong> had multiple users but did not need multiple owners. Here, readers need more than access: each reader must be able to keep the particular snapshot it received alive even if the publisher moves on.</p><p>That is the difference between shared access and shared ownership.</p><h3>The subtle failure mode is often delayed destruction</h3><p>Overuse of <strong>shared_ptr</strong> is frequently discussed in terms of reference-counting overhead or cyclic references. Both are real, but neither captures the full architectural cost.</p><p>Consider an object that owns a socket, file descriptor, thread, timer, callback registration, or another resource whose destruction changes runtime behavior. If a component that only needed access also retains a strong reference, that resource may survive beyond the lifecycle boundary where the system expected it to disappear.</p><p>The destructor may still run eventually. The memory may be reclaimed correctly. There may be no permanent leak.</p><p>The problem is that destruction happens according to the accumulated ownership graph rather than according to the lifecycle boundary the architecture intended.</p><p>EdgeNetSwitch makes the distinction visible at the file-descriptor layer. <strong>UdpPortBackend</strong> stores its native socket inside <strong>FileDescriptor</strong>:</p><pre><br>class UdpPortBackend final : public PortBackend<br>{<br>public:<br>    UdpPortBackend(std::uint32_t port_id, <br>                   const UdpEndpoint &amp;endpoint, <br>                   FdRegistry *registry);<br><br>    TransmitResult transmit(const Packet &amp;packet) override;<br><br>private:<br>    std::uint32_t port_id_;<br>    UdpEndpoint endpoint_;<br>    FileDescriptor socket_;<br>    sockaddr_in destination_{};<br>};</pre><p>The socket resource therefore belongs to the backend through the RAII wrapper. Creating the native socket establishes that ownership directly inside <strong>FileDescriptor</strong>:</p><pre>UdpPortBackend::UdpPortBackend(std::uint32_t port_id, <br>                               const UdpEndpoint &amp;endpoint,<br>                               FdRegistry *fd_registry)<br>    : port_id_(port_id), <br>      endpoint_(endpoint),<br>      socket_(::socket(AF_INET, SOCK_DGRAM, 0),<br>              fd_registry, <br>              FdType::UdpSocket)</pre><p>This matters because closing the descriptor is attached to a well-defined resource owner. Destroying or resetting the <strong>FileDescriptor</strong> controls the native descriptor lifecycle.</p><p>The registry serves a different purpose.</p><h3>Observation is not ownership</h3><p><strong>FileDescriptor</strong> also knows about <strong>FdRegistry</strong>, but the relationship is intentionally non-owning:</p><pre>class FileDescriptor<br>{<br>public:<br>    FileDescriptor() noexcept = default;<br>    FileDescriptor(int fd) noexcept;<br>    FileDescriptor(int fd, FdRegistry *registry, FdType fdType) noexcept;<br>    ~FileDescriptor();<br><br>    // Prevent copying: multiple objects must not own the same FD.<br>    FileDescriptor(const FileDescriptor &amp;) = delete;<br><br>    // Prevent assignment copying: avoids FD leaks and double-close bugs.<br>    FileDescriptor &amp;operator=(const FileDescriptor &amp;) = delete;<br><br>    FileDescriptor(FileDescriptor &amp;&amp;other) noexcept;<br>    FileDescriptor &amp;operator=(FileDescriptor &amp;&amp;other) noexcept;<br><br>    [[nodiscard]] int get() const noexcept;<br>    [[nodiscard]] bool valid() const noexcept;<br><br>    int release() noexcept;<br>    void reset(int fd = -1, FdType type = FdType::Unknown) noexcept;<br><br>private:<br>    int fd_{-1};<br>    FdRegistry *registry_{nullptr};<br>    FdType fd_type_{FdType::Unknown};<br>};</pre><p>The raw pointer is useful precisely because <strong>FileDescriptor</strong> is not supposed to control the registry’s lifetime.</p><p>The descriptor wrapper owns one thing: the native descriptor represented by <strong>fd_</strong>. The registry observes descriptor lifecycle so the runtime can expose states such as <strong>active</strong>, <strong>closed</strong>, or <strong>released</strong>. Making <strong>registry_</strong> a <strong>shared_ptr&lt;FdRegistry&gt; </strong>would not make the descriptor safer. It would give every <strong>FileDescriptor</strong> the ability to keep the registry alive.</p><p>Using a <strong>unique_ptr&lt;FdRegistry&gt; </strong>would be even more incorrect because it would imply exclusive ownership. The raw pointer therefore expresses something that neither owning smart pointer expresses correctly: <strong>FileDescriptor</strong> may talk to this object, but it does not own it.</p><p>This is why replacing every raw pointer in modern C++ with a smart pointer is not a useful design goal. Smart pointers describe ownership. Some relationships are deliberately not ownership relationships.</p><p>The requirement is that the external lifetime guarantee is real. A non-owning pointer does not create safety by itself; the architecture must guarantee that the observed object remains valid for as long as the pointer can be used.</p><p>That requirement is not a weakness of raw pointers. It is the nature of non-owning access.</p><h3>A shared_ptr cycle can keep objects alive</h3><p>A <strong>shared_ptr</strong> destroys an object only when its last strong owner disappears. That creates a problem when two objects own each other.</p><p>Consider a <strong>Parent</strong> that owns a <strong>Child</strong>. The child also needs to reach its parent:</p><pre>struct Child;<br><br>struct Parent {<br>    std::shared_ptr&lt;Child&gt; child;<br>};<br><br>struct Child {<br>    std::shared_ptr&lt;Parent&gt; parent;<br>};</pre><p>The intended relationship is easy to understand: the parent owns the child, while the child needs a way to refer back to the parent.</p><p>Now create both objects:</p><pre>auto parent = std::make_shared&lt;Parent&gt;();<br>auto child = std::make_shared&lt;Child&gt;();</pre><p><strong>parent</strong> is a <strong>shared_ptr&lt;Parent&gt;</strong> that owns the newly created <strong>Parent</strong> object. At this point, the <strong>Parent</strong> object’s <strong>child</strong> member is empty.</p><p>Similarly, <strong>child</strong> is a <strong>shared_ptr&lt;Child&gt;</strong> that owns the newly created <strong>Child</strong> object, and the <strong>Child</strong> object’s <strong>parent</strong> member is empty.</p><p>So before we connect them, there is one strong owner for each object:</p><pre>parent  --&gt; Parent<br>child   --&gt; Child</pre><p>Now connect the objects:</p><pre>parent -&gt; child  = child;<br>child  -&gt; parent = parent;</pre><p>The first line:</p><pre>parent -&gt; child  = child;</pre><p>copies the <strong>shared_ptr&lt;Child&gt;</strong> stored in the local variable <strong>child</strong> into the <strong>child</strong> member of the <strong>Parent</strong> object:</p><pre>struct Parent {<br>    std::shared_ptr&lt;Child&gt; child;<br>};</pre><p>So the same <strong>Child</strong> object is now owned from two places:</p><pre>auto child = std::make_shared&lt;Child&gt;();<br>                |<br>                v<br>              Child<br>                ^<br>                |<br>          Parent::child<br>      (std::shared_ptr&lt;Child&gt;)</pre><p>No second <strong>Child</strong> object is created. The <strong>shared_ptr</strong> is copied, so the strong owner count of the same <strong>Child</strong> object increases.</p><p>The second line:</p><pre>child-&gt;parent = parent;</pre><p>does the same thing for the <strong>Parent</strong> object. It copies the local <strong>shared_ptr&lt;Parent&gt;</strong> into this member of <strong>Child</strong>:</p><pre>struct Child {<br>    std::shared_ptr&lt;Parent&gt; parent;<br>};</pre><p>The same <strong>Parent</strong> object is now also owned from two places:</p><pre>auto parent = std::make_shared&lt;Parent&gt;();<br>                 |<br>                 v<br>               Parent<br>                 ^<br>                 |<br>            Child::parent<br>       (std::shared_ptr&lt;Parent&gt;)</pre><p>Now suppose the code that created them no longer needs them:</p><pre>parent.reset();<br>child.reset();</pre><p><strong>parent.reset() </strong>removes the local variable’s ownership of <strong>Parent</strong>, but <strong>Parent</strong> is not destroyed. <strong>Child::parent</strong> still owns it.</p><p>Likewise, <strong>child.reset() </strong>removes the local variable’s ownership of <strong>Child</strong>, but <strong>Child</strong> is not destroyed. <strong>Parent::child </strong>still owns it.</p><p>At that point, the local owners are gone, but the two objects still own each other.</p><pre>Parent -------owns-------&gt; Child<br>  ^                           |<br>  |                           |<br>  |                           |<br>  +-----------owns------------+</pre><p>The <strong>Parent</strong> cannot be destroyed because the <strong>Child</strong> owns it. The <strong>Child</strong> cannot be destroyed because the <strong>Parent</strong> owns it.</p><p>Each object therefore keeps the other object’s strong reference count above zero.</p><p>That is a <strong>shared_ptr</strong> cycle.</p><p>At this point, the code has created two different lifetime relationships:</p><pre>parent-&gt;child = child;</pre><p>means that <strong>Parent::child</strong> is a strong owner of the <strong>Child</strong>. Even if the local <strong>child</strong> variable disappears, the <strong>Parent</strong> can keep the <strong>Child</strong> alive. That matches the intended relationship: the parent’s ownership is one reason the child should continue to exist.</p><p>But this line:</p><pre>child-&gt;parent = parent;</pre><p>also creates a strong owner. Because <strong>Child::parent</strong> is a <strong>shared_ptr&lt;Parent&gt;</strong>, the child can now keep the parent alive as well.</p><p>That is stronger than what the child actually needs.</p><p>The child may need to call a function on its parent or read information from it, but that does not mean the child should be able to prevent the parent from being destroyed. If the rest of the program no longer owns the parent, the child’s back-reference should not be enough to keep the parent alive.</p><p>So the two directions require different semantics: <strong>Parent</strong> needs ownership of <strong>Child</strong> , while <strong>Child</strong> only needs a non-owning reference back to <strong>Parent</strong>.</p><p><strong>weak_ptr </strong>provides exactly that second relationship.</p><h3>weak_ptr allows access without keeping the object alive</h3><p>Change only the back-reference:</p><pre>struct Child;<br><br>struct Parent {<br>    std::shared_ptr&lt;Child&gt; child;<br>};<br><br>struct Child {<br>    std::weak_ptr&lt;Parent&gt; parent;<br>};</pre><p>The parent still owns the child:</p><pre>parent-&gt;child = child;</pre><p>But this assignment now behaves differently:</p><pre>child-&gt;parent = parent;</pre><p><strong>Child::parent </strong>becomes a <strong>weak_ptr</strong> referring to the <strong>Parent</strong>, but it does not become another strong owner. In other words, the child remembers the parent without contributing to the count that keeps the parent alive.</p><p>This creates an important consequence: the child can no longer assume that its parent still exists.</p><p>For example, some other part of the program could release the last <strong>shared_ptr&lt;Parent&gt;</strong>. The <strong>Parent</strong> would then be destroyed even though <strong>Child::parent </strong>still contains a weak reference to it. That is intentional: a <strong>weak_ptr</strong> is not supposed to keep the object alive.</p><p>This is why a <strong>weak_ptr</strong> cannot simply be used like a <strong>shared_ptr:</strong></p><pre>// Not how weak_ptr is used:<br>parent-&gt;notify();</pre><p>If the <strong>Parent</strong> has already been destroyed, <strong>lock()</strong> returns an empty <strong>shared_ptr&lt;Parent&gt;</strong>. An empty <strong>shared_ptr</strong> converts to <strong>false</strong>, so the body of the <strong>if</strong> is skipped.</p><p>If the <strong>Parent</strong> is still alive, <strong>lock()</strong> returns a new <strong>shared_ptr&lt;Parent&gt;</strong> that shares ownership of the existing <strong>Parent</strong>. The local variable named <strong>parent</strong> therefore becomes a temporary strong owner.</p><p>Suppose the program only checked whether the <strong>Parent</strong> still existed and then tried to use it afterward. Another thread could release the final <strong>shared_ptr&lt;Parent&gt;</strong> between those two operations. The first check would have succeeded, but the <strong>Parent</strong> could already be gone by the time <strong>notify() </strong>was called.</p><p><strong>lock() </strong>avoids that gap:</p><pre>if (auto parent = this-&gt;parent.lock()) {<br>    parent-&gt;notify();<br>}</pre><p>If <strong>lock()</strong> succeeds, the returned <strong>shared_ptr&lt;Parent&gt;</strong> immediately becomes another strong owner of the same <strong>Parent</strong>. That means the object cannot be destroyed while this local <strong>parent</strong> variable still exists.</p><p>The sequence is therefore:</p><pre>Child has a weak_ptr to Parent<br>        |<br>        v<br>      lock()<br>        |<br>        +-- Parent already destroyed --&gt; empty shared_ptr<br>        |<br>        +-- Parent still alive --------&gt; temporary shared_ptr<br>                                             |<br>                                             v<br>                                     Parent stays alive<br>                                     while it is being used</pre><p>So when this line runs:</p><pre>parent-&gt;notify();</pre><p>the call is made through a real <strong>shared_ptr&lt;Parent&gt;</strong>, not through the <strong>weak_ptr</strong>. The temporary <strong>shared_ptr</strong> keeps the object alive for the duration of the <strong>if</strong> block.</p><p>When execution leaves the block, that temporary <strong>shared_ptr</strong> is destroyed and its ownership is released:</p><pre>if (auto parent = this-&gt;parent.lock()) {<br>    parent-&gt;notify();<br>} // temporary shared_ptr is destroyed here</pre><p>This explains why <strong>lock()</strong> does more than check whether the object exists. It converts a non-owning reference into temporary ownership for exactly the period in which the object needs to be used.</p><p>The resulting relationship is now different in each direction. <strong>Parent</strong> keeps <strong>Child</strong> alive through <strong>shared_ptr</strong>, while <strong>Child</strong> can reach <strong>Parent</strong> through <strong>weak_ptr</strong> without permanently extending the parent’s lifetime.</p><h3>Pointer types should encode decisions, not replace them</h3><p>The examples point to the same rule: pointer types describe lifetime relationships. <strong>unique_ptr</strong> can make exclusive ownership and ownership transfer explicit. <strong>shared_ptr</strong> allows several owners to keep the same object alive. <strong>weak_ptr</strong> allows access to an object managed through shared ownership without keeping it alive.</p><p>But those types cannot decide which relationship the program actually needs. That comes from the design: who should keep the object alive, whether ownership should move, and whether multiple components genuinely need to extend the same lifetime.</p><p>Once those decisions are clear, the pointer type is usually the easier part. Smart pointers are powerful because they can enforce an ownership model in code. They cannot decide whether that model is the right one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=540d4199ddcf" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/ownership-comes-before-smart-pointers-540d4199ddcf">Ownership Comes Before Smart Pointers</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAII Is Not About Cleanup. It Is About Ownership.]]></title>
            <link>https://levelup.gitconnected.com/raii-is-not-about-cleanup-it-is-about-ownership-48ad478e302c?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/48ad478e302c</guid>
            <category><![CDATA[systems-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sun, 23 Aug 2026 15:43:29 GMT</pubDate>
            <atom:updated>2026-08-24T14:24:54.538Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vbAfyEZiPHMm41UXwjTiWg.png" /></figure><h3>A file descriptor is not just a number</h3><p>A file descriptor looks deceptively simple. At the operating system boundary, it is represented by an integer returned from an API such as <strong>socket()</strong>. The application can store this value, pass it between functions, and use it with other system calls. Nothing in the integer itself indicates that the descriptor represents an open kernel resource that must eventually be released.</p><pre>int fd = socket(AF_INET, SOCK_DGRAM, 0);</pre><p>However, the integer itself does not describe the resource lifecycle behind it. It does not tell the program which component owns the descriptor, who is responsible for releasing it, whether ownership can be transferred, or whether the underlying resource is still valid. The operating system provides a handle, but the application must define the ownership model around that handle.</p><p>This distinction becomes more important as systems evolve. A small program may manage a resource with a simple sequence: acquire, use, and release. However, long-running systems contain multiple execution paths, failure scenarios, concurrent components, and shutdown procedures. A resource may be created successfully but fail during later initialization. Ownership may need to move between components. Cleanup may need to happen even when control flow leaves unexpectedly.</p><p>The difficulty is therefore not only releasing resources. The deeper problem is keeping ownership and lifetime rules consistent throughout the system.</p><h3>The hidden complexity behind resource management</h3><p>Resource management is often described as a simple lifecycle:</p><pre>Acquire resource → Use resource → Release resource</pre><p>This model works when the lifetime of a resource is controlled by a single execution path.</p><p>However, long-running systems introduce more complex lifecycles. A resource may need additional initialization after creation. It may be passed to another component. It may need to remain available until shutdown. Each of these transitions introduces another decision about who is responsible for the resource at that moment.</p><p>Consider a networking component that creates a socket:</p><pre>int fd = socket(AF_INET, SOCK_DGRAM, 0);<br><br>if (fd == -1)<br>{<br>    return false;<br>}<br><br>if (!configureSocket(fd))<br>{<br>    close(fd);<br>    return false;<br>}<br><br>registerSocket(fd);</pre><p>At first, the ownership appears straightforward. The function creates the socket and releases it if initialization fails.</p><p>The ambiguity begins when the resource moves beyond this function boundary.</p><pre>void registerReceiver(int fd)<br>{<br>    receivers.push_back(fd);<br>}</pre><p>The code does not reveal the ownership relationship anymore.</p><p>Does <strong>registerReceiver() </strong>only borrow the descriptor?</p><p>Does it become responsible for releasing it later?</p><p>Can the original owner still close it?</p><p>The integer value itself cannot answer these questions.</p><p>As systems grow, resource management becomes difficult not because releasing a resource is complicated, but because ownership decisions become distributed across different parts of the codebase. Different components may hold different assumptions about the same resource lifetime.</p><p>The real challenge is keeping those ownership and lifetime rules consistent as resources move through the system.</p><h3>Moving ownership into the object model</h3><p>RAII is often introduced as a mechanism where constructors acquire resources and destructors release them. While this describes the implementation technique, it does not fully explain why RAII is valuable in systems programming.</p><p>The important design decision is that ownership becomes part of the object’s lifetime.</p><p>Instead of representing a resource as a raw handle:</p><pre>int fd;</pre><p>a system can represent the ownership relationship explicitly:</p><pre>FileDescriptor fd;</pre><p>These two variables may eventually refer to the same operating system resource, but they communicate completely different guarantees.</p><p>The integer only stores an identifier. It does not express whether the value is valid, who should release it, or whether another component can safely take ownership of it.</p><p>A resource-owning object defines these rules around that identifier. It can represent ownership, restrict copying, allow controlled transfer, and release the underlying resource when the object’s lifetime ends.</p><h3>A File Descriptor Is a Lifetime Contract</h3><p>A raw file descriptor provides access to a kernel resource, but it does not describe the rules around that resource. The code using the descriptor must still answer questions that are not visible from the value itself: who owns it, whether it can be copied, when it should be released, and how ownership moves between components.</p><p>A resource-owning type changes this relationship. Instead of passing around a value that represents a resource, the program can create an object whose lifetime is connected to the lifetime of that resource.</p><p>In <a href="https://github.com/togunchan/EdgeNetSwitch">EdgeNetSwitch</a>, <a href="https://github.com/togunchan/EdgeNetSwitch/blob/main/src/system/fd/FileDescriptor.cpp"><strong>FileDescriptor</strong></a> was introduced to make this ownership model explicit. The purpose of the class is not only to call <strong>close() </strong>automatically. Its responsibility is to define the rules around descriptor ownership.</p><p>A simplified example shows where the ambiguity appears:</p><pre>int fd = socket(AF_INET, SOCK_DGRAM, 0);<br><br>receiver.attach(fd);</pre><p>At this point, the code does not reveal the ownership relationship.</p><ul><li>Does receiver only use the descriptor while the component that created it remains responsible for releasing it?</li><li>Does receiver take ownership and become responsible for releasing it?</li><li>Should the creating component stop using the descriptor after this call?</li></ul><p>The integer value itself cannot express these decisions.</p><p>A resource-owning type can make this relationship explicit:</p><pre>FileDescriptor fd{<br>    socket(AF_INET, SOCK_DGRAM, 0)<br>};<br><br>receiver.attach(std::move(fd));</pre><p>Now the operation communicates a different intent: ownership is transferred rather than a raw value being passed around.</p><h3>Ownership Must Have Rules</h3><p>A resource wrapper becomes valuable when it defines what operations are allowed and what operations are invalid.</p><p>For a unique operating system resource, copying is usually not a valid operation.</p><p>If two objects appear to own the same descriptor, both objects may eventually try to release the same resource.</p><pre>FileDescriptor first = createSocket();<br><br>FileDescriptor second = first; // This operation is intentionally rejected by the type.</pre><p>The problem is not copying an integer.</p><p>The problem is creating two possible owners for a single resource.</p><p>For this reason, <a href="https://github.com/togunchan/EdgeNetSwitch/blob/main/include/edgenetswitch/system/fd/FileDescriptor.hpp"><strong>FileDescriptor</strong></a> disables copying and allows ownership transfer instead.</p><pre>FileDescriptor second = std::move(first);</pre><p>After this operation, ownership has moved from one object to another. The resource still has a single owner, but the owner has changed.</p><p>This distinction is fundamental in systems programming.</p><p>Copying duplicates the representation of ownership. Moving transfers ownership responsibility.</p><h3>A Destructor Is a Lifetime Boundary</h3><p>Unique ownership solves only part of the lifecycle problem. Once the program knows which object owns a resource, it still needs a reliable rule for when that ownership ends.</p><p>In <a href="https://github.com/togunchan/EdgeNetSwitch/blob/dd21dd1ff55bc3c4715ead2e7d3f2259635d443d/src/system/fd/FileDescriptor.cpp"><strong>FileDescriptor</strong></a>, that boundary is the lifetime of the owning object. The destructor does not contain a separate cleanup sequence. It delegates the transition to <strong>reset()</strong>:</p><pre>FileDescriptor::~FileDescriptor()<br>{<br>    reset();<br>}</pre><p>The parameterless call is intentional. <strong>reset()</strong> declares default arguments:</p><pre>void reset(<br>    int fd = -1,<br>    FdType type = FdType::Unknown<br>) noexcept;</pre><p>Calling <strong>reset()</strong> from the destructor is therefore equivalent to resetting the object to an invalid descriptor. If the object currently owns a valid descriptor, <strong>reset()</strong> records the lifecycle transition in the registry, removes the descriptor from tracking, and releases the kernel resource with <strong>close()</strong>.</p><pre>void FileDescriptor::reset(int fd, FdType type) noexcept<br>{<br>    if (fd_ &gt;= 0)<br>    {<br>        if (registry_)<br>        {<br>            registry_-&gt;updateState(fd_, FdState::Closed);<br>            registry_-&gt;unregisterFd(fd_);<br>        }<br><br>        ::close(fd_);<br>    }<br><br>    fd_ = fd;<br><br>    if (registry_ &amp;&amp; fd_ &gt;= 0)<br>    {<br>        registry_-&gt;registerFd(fd_, FdState::Active, type);<br>    }<br>}</pre><p>When the destructor invokes <strong>reset()</strong> without arguments, the new value assigned to <strong>fd_</strong> is <strong>-1</strong>. No new descriptor is registered, so destruction leaves no resource owned by the object.</p><p>The important behavior is not merely that <strong>close()</strong> happens automatically. The stronger property is that the release policy belongs to the object that owns the descriptor. As long as ownership remains with <strong>FileDescriptor</strong>, callers do not need a separate rule requiring the descriptor to be closed before the owner disappears. Destruction already defines that boundary.</p><p>The difference becomes easier to see with a conceptual manual-cleanup example. This is not EdgeNetSwitch code; it illustrates the control-flow problem that an owning type avoids:</p><pre>int fd = socket(AF_INET, SOCK_DGRAM, 0);<br><br>if (fd == -1)<br>{<br>    return false;<br>}<br><br>if (!configureSocket(fd))<br>{<br>    close(fd);<br>    return false;<br>}<br><br>if (!registerSocket(fd))<br>{<br>    close(fd);<br>    return false;<br>}</pre><p>Here, every failure path after acquisition must preserve the same lifecycle rule: the function acquired the descriptor, so leaving the function before ownership is transferred requires releasing it. Adding another initialization step means adding another path that must remember the same rule.</p><p>With an owning object, that rule is attached to object lifetime rather than repeated in each branch. The point is not that RAII removes failure handling. The program still has to decide whether configuration, registration, or any later operation succeeded. What RAII removes is the need for those failure paths to independently reproduce the resource-release policy.</p><p>That is why the destructor is more than a convenient place for cleanup code. For a resource-owning object, destruction defines the final boundary beyond which that ownership cannot continue.</p><h3>Releasing Ownership Is Not the Same as Releasing the Resource</h3><p>Destruction defines the final ownership boundary for <strong>FileDescriptor</strong>, but the type also exposes a different transition: giving up ownership without closing the underlying descriptor.</p><p>EdgeNetSwitch implements this through <strong>release()</strong>:</p><pre>int FileDescriptor::release() noexcept<br>{<br>    const int old_fd = fd_;<br><br>    if (registry_ &amp;&amp; old_fd &gt;= 0)<br>    {<br>        registry_-&gt;updateState(old_fd, FdState::Released);<br>    }<br><br>    fd_ = -1;<br><br>    return old_fd;<br>}</pre><p>The important detail is that <strong>release()</strong> does not call <strong>close()</strong>.</p><p>Instead, it returns the descriptor value and moves the <strong>FileDescriptor</strong> itself into its non-owning state by setting <strong>fd_</strong> to <strong>-1</strong>. The kernel resource may still remain open, but this object is no longer responsible for its lifetime.</p><p>That makes <strong>release()</strong> fundamentally different from <strong>reset()</strong>.</p><pre>reset()   --&gt; release the resource currently owned by the object<br><br>release() --&gt; stop owning the resource without closing it</pre><p>This distinction matters because ownership and resource existence are not the same thing. A resource can continue to exist after one owner stops managing it. In a larger system, such a transition could be useful when responsibility must move to another ownership mechanism or to an API whose contract takes over the raw handle.</p><p>In the current EdgeNetSwitch runtime, however, <strong>release() </strong>is not used by the production code paths. Its behavior is covered by tests, so the ownership transition is implemented and validated, but it should not be presented as an active runtime requirement.</p><p>That limitation is useful in itself. The method shows that the ownership model was designed to distinguish between two separate events: destroying a resource and relinquishing responsibility for it.</p><p>The registry records the same distinction:</p><pre>registry_-&gt;updateState(old_fd, FdState::Released);</pre><p>A released descriptor is therefore not treated as a closed descriptor. The registry records that <strong>FileDescriptor</strong> has relinquished responsibility for the resource, while the underlying kernel object may continue to exist.</p><p>The effect becomes visible when the <strong>FileDescriptor</strong> itself is later destroyed. Because <strong>release()</strong> sets <strong>fd_</strong> to <strong>-1</strong>, the destructor eventually calls <strong>reset()</strong> with an already non-owning object:</p><pre>FileDescriptor::~FileDescriptor()<br>{<br>    reset();<br>}</pre><p>Inside <strong>reset()</strong>, the cleanup path is guarded by the current descriptor value:</p><pre>if (fd_ &gt;= 0)<br>{<br>    ::close(fd_);<br>}</pre><p>After <strong>release()</strong>, that condition is false because <strong>fd_ == -1</strong>. Destruction therefore does not call <strong>close()</strong> on the descriptor that was previously owned by the object.</p><p>This behavior keeps ownership state consistent across the object’s lifetime. Once <strong>release()</strong> ends the ownership relationship, later destruction must not act on that resource again. The resource may continue to exist, but <strong>FileDescriptor</strong> is no longer responsible for managing it..</p><h3>Ownership and Observability Are Different Responsibilities</h3><p>Once resource ownership becomes explicit, another architectural question appears: should the object that owns the resource also be responsible for making its lifecycle observable?</p><p>EdgeNetSwitch keeps these responsibilities separate. <strong>FileDescriptor</strong> owns the descriptor and controls the operations that can end or transfer that ownership. <strong>FdRegistry</strong> observes lifecycle transitions, but it does not decide when the descriptor should be closed.</p><p>This separation is visible in the <strong>FileDescriptor</strong> constructor:</p><pre>    FileDescriptor::FileDescriptor(int fd, FdRegistry *registry, FdType fdType) noexcept<br>        : fd_(fd), registry_(registry), fd_type_(fdType)<br>    {<br>        if (registry_ &amp;&amp; valid())<br>            registry_-&gt;registerFd(fd, FdState::Active, fdType);<br>    }</pre><p>A valid descriptor is registered as <strong>FdState::Active</strong>, but registration does not transfer ownership to the registry. The descriptor still belongs to the <strong>FileDescriptor</strong> object.</p><p>The same relationship appears when the resource is closed:</p><pre>        if (fd_ &gt;= 0)<br>        {<br>            if (registry_)<br>            {<br>                registry_-&gt;updateState(fd_, FdState::Closed);<br>                registry_-&gt;unregisterFd(fd_);<br>            }<br><br>            ::close(fd_);<br>        }</pre><p>The registry records what is happening, while <strong>FileDescriptor</strong> performs the operation that actually releases the kernel resource.</p><p>This distinction becomes even clearer with <strong>release()</strong>:</p><pre>        const int old_fd = fd_;<br><br>        if (registry_ &amp;&amp; old_fd &gt;= 0)<br>        {<br>            registry_-&gt;updateState(old_fd, FdState::Released);<br>        }<br><br>        fd_ = -1;</pre><p>Here the registry records that ownership has been relinquished. It does not close the descriptor and it does not become the new owner. The lifecycle state changes because the ownership relationship changed, not because the registry performed a resource operation.</p><p>This separation matters because observability should not silently become ownership. A component that tracks resource state may need to answer questions such as which descriptors are active, which have been released, or are no longer tracked. None of those responsibilities require it to control the resource itself.</p><p>The ownership boundary therefore remains simple:</p><pre>FileDescriptor  --&gt; owns and manages the descriptor<br><br>FdRegistry      --&gt; observes descriptor lifecycle</pre><p>Keeping ownership and observability separate also prevents two components from gaining lifecycle authority over the same descriptor. If <strong>FdRegistry</strong> could both track and close descriptors, it would no longer be a passive observer; it could change the lifecycle it is supposed to report.</p><h3>A Mutex Uses the Same Lifetime Idea Differently</h3><p>File descriptors make ownership easy to see because the resource has an explicit handle and an explicit release operation. A mutex is different. The important resource is not the mutex object itself but the period during which a thread owns its lock.</p><p>With manual locking, that ownership has to be released explicitly.</p><pre>mutex.lock();<br><br>updateSharedState();<br><br>mutex.unlock();</pre><p>This works only if execution always reaches <strong>unlock()</strong>.</p><p>A small change can break that assumption:</p><pre>mutex.lock();<br><br>if (!ready)<br>{<br>    return; // mutex remains locked<br>}<br><br>updateSharedState();<br><br>mutex.unlock();</pre><p>The early return leaves the function before <strong>unlock()</strong> is called. The thread still owns the lock, so another thread that later tries to acquire the same mutex may remain blocked.</p><p>An exception creates the same problem:</p><pre>mutex.lock();<br><br>updateSharedState(); // may throw<br><br>mutex.unlock();</pre><p>If <strong>updateSharedState()</strong> throws, execution again leaves the protected region before the mutex is released.</p><p>The problem is therefore not that <strong>unlock()</strong> is difficult to call. The problem is that manual locking makes lock release depend on every possible exit path remembering to call it.</p><p>A scoped lock changes that relationship:</p><pre>{<br>    std::lock_guard&lt;std::mutex&gt; lock{mutex};<br><br>    if (!ready)<br>    {<br>        return;<br>    }<br><br>    updateSharedState();<br>}</pre><p>When <strong>lock</strong> is created, the current thread acquires the mutex. When <strong>lock</strong> leaves scope, its destructor releases it. That happens whether the scope ends normally, through an early return, or because an exception propagates out of the scope.</p><p>The lifetime of the guard therefore defines the lifetime of lock ownership.</p><pre>                                       |                  |<br>lock_guard created --&gt; mutex locked --&gt;| PROTECTED REGION | --&gt;lock_guard destroyed --&gt; mutex unlocked<br>                                       |                  |</pre><p>This is the same ownership idea used by <strong>FileDescriptor</strong>, but applied to a different kind of resource.</p><p>For a file descriptor:</p><pre>FileDescriptor lifetime --&gt; descriptor ownership --&gt; destructor --&gt; close()</pre><p>For a mutex lock:</p><pre>lock_guard lifetime --&gt; lock ownership --&gt; destructor --&gt; unlock()</pre><p>RAII does not require every resource to have the same cleanup operation. What remains consistent is the relationship between ownership and object lifetime.</p><p>The mutex example also shows an important limitation. <strong>lock_guard</strong> guarantees that the lock will be released when its scope ends, but it does not decide whether the scope itself is well designed.</p><p>A program can still hold the lock for too long:</p><pre>{<br>    std::lock_guard&lt;std::mutex&gt; lock{mutex};<br><br>    updateSharedState();<br>    performSlowOperation();<br>    callExternalCode();<br>}</pre><p>The lock is released correctly, but other threads may be blocked for much longer than necessary. RAII solves the release problem; it does not choose the correct critical-section boundary.</p><p>EdgeNetSwitch provides a concrete example of this boundary in <strong>MessagingBus::publish()</strong>:</p><pre>    void MessagingBus::publish(const Message &amp;message)<br>    {<br>        std::vector&lt;Callback&gt; callbacks;<br><br>        {<br>            std::lock_guard&lt;std::mutex&gt; lock(mutex_);<br>            auto it = subscribers_.find(message.type);<br>            if (it != subscribers_.end())<br>            {<br>                callbacks = it-&gt;second; // copy callbacks for safe iteration<br>            }<br>        }<br><br>        for (const auto &amp;cb : callbacks)<br>        {<br>            cb(message);<br>        }<br>    }</pre><p>The mutex is held only while <strong>subscribers_</strong> is accessed and the relevant callback list is copied. When the inner scope ends, the <strong>lock_guard</strong> is destroyed and the mutex is released.</p><p>The callbacks are then invoked outside the critical section. This is important because callback execution may perform additional work or enter other parts of the system. Holding <strong>mutex_</strong> during that work would extend the critical section beyond the shared state it is intended to protect.</p><p>The important design decision is therefore not only that <strong>std::lock_guard </strong>releases the mutex automatically. Its lifetime also defines exactly how long lock ownership lasts. RAII guarantees the release at the end of that lifetime; the programmer still decides where that lifetime should begin and end.</p><h3>Thread Lifetime Requires a Shutdown Protocol</h3><p>A thread introduces a different lifetime problem. A file descriptor can usually be released with a single operation such as <strong>close()</strong>, and a mutex lock can be released with <strong>unlock()</strong>. A running thread is different because it may still be executing code or waiting for work while the object that owns it is being destroyed.</p><p>EdgeNetSwitch encounters this problem in <strong>PacketProcessor</strong>. The component starts a worker thread in its constructor:</p><pre>worker_ = std::thread([this]() { processLoop(); });</pre><p>The lambda captures <strong>this</strong>, which means the worker executes processLoop() through the <strong>PacketProcessor</strong> object itself. As long as that worker is running, the object state it may access must remain valid.</p><p>Destroying the object while the worker is still executing would violate that dependency. The destructor therefore cannot treat the thread like a resource that can be released with a single call.</p><p><strong>PacketProcessor</strong> instead performs an ordered shutdown:</p><pre>PacketProcessor::~PacketProcessor()<br>{<br>    running_.store(false, std::memory_order_relaxed);<br>    cv_.notify_all();<br><br>    if (worker_.joinable())<br>        worker_.join();<br>}</pre><p>Each operation solves a different part of the shutdown problem.</p><p>First:</p><pre>running_.store(false, std::memory_order_relaxed);</pre><p>changes the state observed by the processing loop so that the worker can stop.</p><p>But changing the flag alone may not be sufficient. If the worker is currently waiting on the condition variable, it must first wake up before it can observe the shutdown state. The destructor therefore follows with:</p><pre>cv_.notify_all();</pre><p>Finally:</p><pre>if (worker_.joinable())<br>    worker_.join();</pre><p>waits until the worker thread has actually finished execution.</p><p>The sequence therefore looks like this:</p><pre>PacketProcessor destruction begins --&gt; running_ = false --&gt; wake waiting worker --&gt; worker observes shutdown --&gt; worker exits processLoop() --&gt; join() returns --&gt; PacketProcessor destruction can complete</pre><p>The distinction between requesting termination and completing termination is important. Setting <strong>running_</strong> to <strong>false</strong> expresses the request that the worker stop. <strong>join()</strong> establishes that the worker has actually stopped before destruction proceeds beyond the thread-owning component.</p><p>This is different from the earlier <strong>FileDescriptor</strong> example. For a file descriptor, the ownership boundary can end directly with <strong>close()</strong>.</p><p>For the worker thread, destruction requires several coordinated transitions:</p><pre>request stop --&gt; wake worker --&gt; wait for worker exit</pre><p>A thread needs a different shutdown rule because it is still executing code. Destroying <strong>PacketProcessor</strong> before its worker stops could leave the worker running while the object’s members are being destroyed.</p><p>That is why the destructor does more than release a handle. It requests shutdown, wakes the worker, and waits with <strong>join()</strong> until execution has finished.</p><p>RAII gives that shutdown process a reliable lifetime boundary: the owner cannot finish destruction until the resource-specific shutdown work is complete.</p><h3>Some Resources Need More Than One Lifetime Boundary</h3><p>A resource-owning type can only manage the resource it actually owns. EdgeNetSwitch’s control socket shows why that distinction matters.</p><p>The control socket begins with a UNIX socket descriptor:</p><pre>const int raw_fd = ::socket(AF_UNIX, SOCK_STREAM, 0);<br>if (raw_fd &lt; 0)<br>{<br>    Logger::error(&quot;Failed to create control socket&quot;);<br>    return {};<br>}<br><br>FileDescriptor control_fd(raw_fd, fd_registry, FdType::UnixSocket);</pre><p>From this point on, <strong>FileDescriptor</strong> is responsible for the descriptor. If the function returns early, the local <strong>control_fd</strong> object is destroyed and its destructor closes the descriptor.</p><p>The control socket, however, also depends on a filesystem pathname:</p><pre>std::strncpy(addr.sun_path, CONTROL_SOCKET_PATH, sizeof(addr.sun_path) - 1);<br><br>// Remove any existing socket file at the same path<br>::unlink(CONTROL_SOCKET_PATH);</pre><p>The old pathname is removed before the socket is bound. Then:</p><pre>if (::bind(control_fd.get(), reinterpret_cast&lt;sockaddr *&gt;(&amp;addr), sizeof(addr)) &lt; 0)<br>{<br>    Logger::error(&quot;Failed to bind the control socket&quot;);<br>    control_fd.reset();<br>    return {};<br>}</pre><p>associates the UNIX socket with <strong>CONTROL_SOCKET_PATH</strong>.</p><p>After a successful <strong>bind()</strong>, the control socket therefore involves two different resources:</p><pre>File descriptor       --&gt; released with close()<br><br>Filesystem pathname   --&gt; removed with unlink()</pre><p>They belong to the same logical control socket, but they do not have the same lifetime or the same release operation.</p><p>This becomes visible in the later failure paths. Consider the <strong>listen()</strong> step:</p><pre>// Allow the socket to accept incoming connections, with a queue size of up to 4<br>if (::listen(control_fd.get(), 4) &lt; 0)<br>{<br>    Logger::error(&quot;Failed to listen on control socket&quot;);<br>    control_fd.reset();<br>    return {};<br>}</pre><p>At this point, <strong>bind()</strong> has already succeeded. Calling <strong>control_fd.reset(); </strong>closes the descriptor, but it does not remove <strong>CONTROL_SOCKET_PATH</strong>.</p><p>The same pattern appears if retrieving or changing the socket flags fails:</p><pre>const int flags = ::fcntl(control_fd.get(), F_GETFL, 0);<br><br>if (flags &lt; 0)<br>{<br>    Logger::error(&quot;Failed to get control socket flags&quot;);<br>    control_fd.reset();<br>    return {};<br>}</pre><p>and:</p><pre>if (::fcntl(control_fd.get(), F_SETFL, flags | O_NONBLOCK) &lt; 0)<br>{<br>    Logger::error(&quot;Failed to enable O_NONBLOCK on control socket&quot;);<br>    control_fd.reset();<br>    return {};<br>}</pre><p>In each case, the descriptor is released, but the pathname created by the earlier successful <strong>bind()</strong> is handled separately.</p><p>The normal shutdown path makes this separation explicit:</p><pre>void destroyControlSocket(FileDescriptor &amp;fd)<br>{<br>    Logger::debug(&quot;[CONTROL] Closing control socket fd=&quot; + std::to_string(fd.get()));<br>    fd.reset();<br>    Logger::debug(&quot;[CONTROL] Control socket close completed&quot;);<br>    ::unlink(CONTROL_SOCKET_PATH);<br>    Logger::info(&quot;Control socket closed&quot;);<br>}</pre><p>Two cleanup operations are required because two resources are involved.</p><p><strong>FileDescriptor</strong> is doing its job correctly: it owns the descriptor and closes it. The pathname is a separate resource, so it needs separate cleanup.</p><p>RAII only manages the lifetime of the resource represented by the owning object. In this case, the operation creates two resources, but only one of them is covered by <strong>FileDescriptor</strong>.</p><h3>Ownership Defines the Lifetime Boundary</h3><p>RAII is often introduced as a way to perform cleanup automatically, but cleanup is only the visible part of the design.</p><p>The deeper change is that ownership becomes connected to object lifetime. The object responsible for a resource also defines when that responsibility begins, how it can move, and how it ends.</p><p>In EdgeNetSwitch, this appeared in different forms:</p><ul><li><strong>FileDescriptor</strong> owns a kernel descriptor and releases it with <strong>close()</strong>.</li><li><strong>lock_guard</strong> owns a critical section and releases the mutex when the scope ends.</li><li><strong>PacketProcessor</strong> owns a worker thread and completes shutdown before destruction continues.</li></ul><p>The release operation changes depending on the resource. The ownership principle does not.</p><p>RAII is valuable because it moves lifetime management away from scattered cleanup rules and places it inside the structure of the program. Instead of relying on every execution path to remember the correct release behavior, the owning object carries that responsibility through its lifetime.</p><p>That is why RAII is more than automatic cleanup. It is a way to make ownership boundaries explicit and keep resource lifetime decisions close to the objects that are responsible for them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48ad478e302c" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/raii-is-not-about-cleanup-it-is-about-ownership-48ad478e302c">RAII Is Not About Cleanup. It Is About Ownership.</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MSG_TRUNC Is Not MTU Problem: A Linux UDP Receive Path Investigation]]></title>
            <link>https://levelup.gitconnected.com/msg-trunc-is-not-mtu-problem-a-linux-udp-receive-path-investigation-f180a0f344dc?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/f180a0f344dc</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[systems-programming]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Thu, 13 Aug 2026 19:38:47 GMT</pubDate>
            <atom:updated>2026-08-14T14:08:07.915Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bkCAMUnmdSaXjfJ-dMisqw.png" /></figure><p>A UDP application can report that something went wrong. The difficult part is determining where that behavior became visible.</p><p>A packet capture may show fragmented IPv4 traffic, a UDP socket may return a complete datagram, and an application may reject the received payload. These observations can appear to describe the same problem: “the packet was too large.” However, they represent different conditions that become visible at different boundaries inside the receive path.</p><p>I encountered this while investigating the Linux UDP receive path of EdgeNetSwitch, a C++20 userspace networking runtime used as an exploration platform for packet processing, observability, and deterministic behavior.</p><p>The goal was not to explain the Linux kernel networking implementation or benchmark socket performance. The goal was to make receive-path behavior visible enough to understand which layer produced each observation.</p><p>The first experiment produced an unexpected result.</p><p>Using a Linux network namespace topology connected through a veth pair with an MTU of 1500 bytes, <strong>tcpdump</strong> showed an oversized IPv4 packet split into two fragments:</p><pre>id 34335, offset 0, flags [+], length 1500<br>id 34335, offset 1480, flags [none], length 409</pre><p>At the same time, the application received:</p><pre>[UDP] recvmsg: len=1861, flags=0x0</pre><p>The packet capture showed two IPv4 fragments, while the UDP socket returned one complete UDP payload. <strong>MSG_TRUNC</strong> was not set.</p><p>The observations were not contradictory. They represented two different points where the same traffic became visible. The packet capture showed the IPv4 fragments present on the network path, while the UDP socket showed the datagram delivered through the socket interface after Linux processed the traffic at that boundary.</p><p>The important distinction was not deciding which observation was correct. Both were valid. The important question was identifying which layer produced the evidence being observed.</p><p>Before changing a buffer size, an MTU value, or an application validation rule, the more useful question was:</p><blockquote>Which layer produced the evidence I am looking at?</blockquote><p>This question became the foundation of the investigation.</p><h3>A “Packet” Is Not One Thing</h3><p>When only one layer matters, calling everything a packet is usually harmless. It becomes ambiguous when multiple layers participate in the same investigation.</p><p>A packet capture, a socket API and an application parser are not observing the same object. They observe different representations of the same traffic as it moves through the system.</p><p>For this receive path, a more useful conceptual model is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6GRAdOH-MEnyDzS9aVl0w.png" /></figure><p>This is not a Linux kernel-source call graph. It is a model of observation boundaries.</p><p>A packet capture can expose IPv4 fragments. A UDP socket exposes datagram-oriented delivery.<strong> recvmsg()</strong> can expose additional information about that delivery through message flags and ancillary data. The application then applies its own rules to the bytes it received.</p><p>The same traffic can therefore produce different observations depending on where it is inspected.</p><p>This changes how receive-path problems should be investigated. Instead of asking why a packet behaves strangely, the more useful question is where that behavior first becomes visible.</p><p>The fragmentation experiment answered that question for the network path. The next step was to understand what socket-level truncation actually represented.</p><h3>Fragmentation Ends Before UDP Delivery</h3><p>The first experiment used a controlled MTU-1500 path. The topology was intentionally simple:</p><pre>root namespace                 mtu-test namespace<br><br>10.10.0.1                      10.10.0.2<br><br>veth-a  MTU 1500  &lt;---------&gt;  MTU 1500  veth-b</pre><p>The path was created with a Linux veth pair:</p><pre>ip netns add mtu-test<br><br>ip link add veth-a type veth peer name veth-b<br><br>ip link set veth-b netns mtu-test<br><br>ip link set veth-a up<br><br>ip netns exec mtu-test ip link set veth-b up</pre><p>Traffic was captured from the root namespace side:</p><pre>tcpdump -ni veth-a -vv &#39;ip&#39;</pre><p>A UDP message larger than the MTU was then sent through the path.</p><p>The capture showed:</p><pre>id 34335, offset 0, flags [+], length 1500<br>id 34335, offset 1480, flags [none], length 409</pre><p>The UDP socket, however, reported:</p><pre>[UDP] recvmsg: len=1861, flags=0x0</pre><p>The capture showed two IPv4 fragments, while the socket received one UDP datagram. In this experiment, IPv4 reassembly had completed before the UDP datagram was delivered through the socket interface.</p><p>The application did not reconstruct fragments because it never received them as fragments.</p><p>This distinction matters because fragmentation and truncation can appear similar from an application perspective, but they originate from different boundaries:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1FWiiu6oEBb2jqrzhCb0Fw.png" /></figure><p>An MTU limitation can result in IPv4 fragmentation. A userspace receive-buffer limitation can result in <strong>MSG_TRUNC</strong>. The symptoms may appear related, but the evidence points to different mechanisms.</p><h3>MSG_TRUNC Starts at a Different Boundary</h3><p>The fragmentation experiment showed that an IPv4 packet can be split on the network path while the UDP socket still receives one complete datagram.</p><p>The next question was different:</p><blockquote>What happens when the datagram reaches the socket, but the userspace receive buffer is not large enough to hold it?</blockquote><p>To isolate this condition, I changed the experiment so that the receive buffer supplied to recvmsg() was intentionally smaller than the incoming datagram.</p><p>The receive call used a 16-byte userspace buffer:</p><pre>std::array&lt;char, 16&gt; buffer{};<br><br>iovec io{};<br>io.iov_base = buffer.data();<br>io.iov_len = buffer.size();<br><br>msghdr message{};<br>message.msg_iov = &amp;io;<br>message.msg_iovlen = 1;<br><br>const ssize_t len =<br>    ::recvmsg(socket_fd_.get(), &amp;message, 0);</pre><p>The result was:</p><pre>[UDP] recvmsg: len=16, flags=0x20<br>[UDP] MSG_TRUNC</pre><p>The important detail was where the truncation became visible.</p><p>Because this receive call did not pass <strong>MSG_TRUNC</strong> as an input flag, only the prefix that fit in the supplied buffer was copied to userspace, while <strong>msg_flags</strong> reported <strong>MSG_TRUNC</strong>.</p><p>The application had not received an incomplete message from the sender.</p><p>The application had received an incomplete view of the datagram because its own receive storage was insufficient.</p><p>This distinction matters because the next layer cannot recover information that never reached it.</p><p>If parsing continued after this point, the runtime could report a protocol parsing failure or an application validation error. However, the earliest layer with enough evidence to explain the failure was the socket receive boundary.</p><p>The runtime therefore changed the classification:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z76gCPpYtnJ4l16J4grwUQ.png" /></figure><p>The purpose was not to add another error counter.</p><p>It was to preserve the reason the datagram became invalid.</p><p>A receive-layer failure should not be disguised as an application-layer failure.</p><h3>A Complete Datagram Can Still Be Rejected</h3><p>The MSG_TRUNC experiment established what happens when the userspace receive buffer is too small for the datagram being delivered. The next experiment removed that limitation so that the application boundary could be observed independently.</p><p>Instead of the deliberately constrained 16-byte buffer, the receive path used a 2048-byte userspace buffer:</p><pre>static constexpr std::size_t UDP_RECEIVE_BUFFER_SIZE = 2048;<br><br>std::array&lt;char, UDP_RECEIVE_BUFFER_SIZE&gt; buffer{};</pre><p>This change affected only the storage supplied to <strong>recvmsg()</strong>. The application’s own payload-size policy remained unchanged.</p><p>With enough userspace capacity to hold the datagram, <strong>recvmsg()</strong> returned:</p><pre>[UDP] recvmsg: len=1261, flags=0x0</pre><p><strong>MSG_TRUNC</strong> was no longer present. The datagram had reached userspace completely.</p><p>The runtime nevertheless rejected the message later:</p><pre>Packet rejected: reason=payload_too_large</pre><p>That rejection came from a different boundary. After reception and parsing, the runtime applied an independent application-level payload limit:</p><pre>static constexpr std::size_t MAX_PAYLOAD_SIZE = 512;<br><br>if (packet.payload.size() &gt; MAX_PAYLOAD_SIZE)<br>{<br>    return {false, PacketRejectReason::PayloadTooLarge};<br>}</pre><p>The 2048-byte userspace receive buffer and the 512-byte application payload limit therefore represented two separate constraints. The receive buffer determined whether <strong>recvmsg()</strong> had enough storage to deliver the datagram completely to userspace. The application limit determined whether the parsed payload was acceptable after reception.</p><p>In this experiment, the first condition was satisfied: recvmsg() returned 1261 bytes with no <strong>MSG_TRUNC</strong>. The second condition was not: the resulting application payload exceeded the runtime’s 512-byte policy and was rejected by PacketValidator.</p><p>This distinction produced three superficially similar but technically separate size-related conditions:</p><pre>IPv4 fragmentation != MSG_TRUNC != payload_too_large</pre><p>All three involve size, but they describe different parts of the receive path. IPv4 fragmentation concerns how an IP packet crosses a path constrained by MTU. <strong>MSG_TRUNC</strong> concerns whether the userspace buffer supplied to <strong>recvmsg() </strong>can hold the delivered UDP datagram. <strong>payload_too_large</strong> concerns an application rule evaluated only after the datagram has been received and parsed.</p><p>The useful question is therefore not merely whether the data was too large, but which size boundary was exceeded first.</p><h3>Why recvmsg() Changed the Observation Model</h3><p>The previous experiments depended on information that was not part of the UDP payload itself. Detecting <strong>MSG_TRUNC</strong> required access to message flags, and the next stage of the investigation required metadata delivered alongside the payload.</p><p>The original receive path used <strong>recvfrom()</strong>, which was sufficient while the runtime only needed the received bytes, the sender address, and the success or failure of the receive operation. Once the investigation began asking why a particular receive behaved differently, that observation model was no longer sufficient.</p><p>The move to <strong>recvmsg()</strong> was therefore not about replacing one socket API with a more advanced one. It was about exposing additional information associated with each receive operation.</p><p>A simplified version of the receive setup looked like this:</p><pre>std::array&lt;char, 2048&gt; buffer{};<br><br>iovec io{};<br>io.iov_base = buffer.data();<br>io.iov_len = buffer.size();<br><br>sockaddr_in client_addr{};<br><br>msghdr message{};<br>message.msg_name = &amp;client_addr;<br>message.msg_namelen = sizeof(client_addr);<br>message.msg_iov = &amp;io;<br>message.msg_iovlen = 1;<br><br>const ssize_t len =<br>    ::recvmsg(socket_fd_.get(), &amp;message, 0);</pre><p>The important part was the <strong>msghdr</strong> structure. It gave the runtime separate places to observe the payload, message flags, and ancillary metadata associated with the receive.</p><p>The <strong>MSG_TRUNC</strong> experiment had already shown why msg_flags mattered. The next question was whether the metadata channel itself could become incomplete.</p><p>Ancillary data is delivered through a control buffer referenced by <strong>msg_control</strong> and <strong>msg_controllen</strong>. The runtime allocated that storage separately from the payload buffer:</p><pre>std::array&lt;std::byte, CMSG_SPACE(sizeof(timespec))&gt; control_buffer{};<br><br>message.msg_control = control_buffer.data();<br>message.msg_controllen = control_buffer.size();</pre><p>This introduced another capacity boundary.</p><p>To make that boundary visible, I deliberately reduced the control buffer length:</p><pre>message.msg_control = control_buffer.data();<br>message.msg_controllen = 1;</pre><p>The receive operation then reported:</p><pre>[UDP] recvmsg: len=77, flags=0x8<br>[UDP] MSG_CTRUNC</pre><p>Unlike the earlier <strong>MSG_TRUNC</strong> experiment, the UDP payload itself was not the part that had been truncated. The insufficient storage was the control buffer used for ancillary metadata.</p><p>That distinction matters because the two flags describe different failures:</p><pre>MSG_TRUNC  -&gt; datagram data did not fit<br><br>MSG_CTRUNC -&gt; ancillary data did not fit</pre><p>In this experiment, the payload continued through the receive path, but the expected ancillary metadata was not available. Once msg_controllen was restored to the actual size of the control buffer, <strong>MSG_CTRUNC</strong> disappeared and the metadata became observable again.</p><p>The important engineering point was that “truncation” was still not a single condition. The payload and the metadata associated with that payload had independent storage boundaries, and <strong>recvmsg() </strong>exposed which one had been exceeded.</p><h3>The Kernel Can Tell You When It Saw the Datagram</h3><p>Once ancillary data was available, it could be used for more than detecting control-buffer truncation.</p><p>The next experiment enabled Linux receive timestamps with <strong>SO_TIMESTAMPNS</strong>:</p><pre>int enable_timestamping = 1;<br><br>if (::setsockopt(socket_fd_.get(),<br>                 SOL_SOCKET,<br>                 SO_TIMESTAMPNS,<br>                 &amp;enable_timestamping,<br>                 sizeof(enable_timestamping)) &lt; 0)<br>{<br>    throw std::runtime_error(<br>        &quot;Failed to enable UDP receive timestamping&quot;);<br>}</pre><p>Linux then attached a timestamp to received datagrams through ancillary data. The runtime inspected the control messages and extracted <strong>SCM_TIMESTAMPNS</strong>:</p><pre>for (cmsghdr* control = CMSG_FIRSTHDR(&amp;message);<br>     control != nullptr;<br>     control = CMSG_NXTHDR(&amp;message, control))<br>{<br>    if (control-&gt;cmsg_level == SOL_SOCKET &amp;&amp; control-&gt;cmsg_type == SCM_TIMESTAMPNS)<br>    {<br>        const auto* kernel_timestamp =<br>            reinterpret_cast&lt;const timespec*&gt;(CMSG_DATA(control));<br>    }<br>}</pre><p>The timestamp used a <strong>timespec</strong> compatible with <strong>CLOCK_REALTIME</strong>, so the userspace comparison needed to use the same clock domain. Immediately after a successful recvmsg(), the runtime sampled a realtime-compatible userspace clock and compared the two timestamps.</p><p>A receive captured during burst traffic produced:</p><pre>[UDP] recvmsg: len=68, flags=0x0<br>[UDP] kernel timestamp: sec=1786198140, nsec=78496304<br>[UDP] packet kernel-to-userspace latency=137942875 ns</pre><p>The measured difference was approximately 137.9 ms.</p><p>Conceptually, the measurement was:</p><pre>kernel receive timestamp -&gt; measured interval -&gt; recvmsg() returns -&gt; userspace timestamp sample</pre><p>I refer to this measurement as the kernel-to-userspace receive interval. That name is deliberately narrow. It is not an end-to-end network latency measurement, a NIC latency measurement, or a direct measurement of time spent only in the socket receive queue. The interval can include several effects between the kernel timestamp and the point where userspace samples the clock, including scheduling and receive-side processing.</p><p>The measurement was still useful because it made another previously invisible behavior observable. During burst traffic, the measured interval increased, including the roughly 138 ms observation above.</p><p>The useful conclusion was not that the socket queue had introduced 138 ms of delay. The experiment did not isolate the interval that precisely. What it showed was that, under this burst workload, substantially more time elapsed between the kernel receive timestamp and userspace servicing the datagram.</p><p>That changes the investigation from a vague statement such as “UDP seems slow under load” into a narrower question:</p><blockquote>Why did userspace service this receive later within the measured kernel-to-userspace interval?</blockquote><p>The same log also contained another useful signal:</p><pre>[UDP] kernel receive queue drops=8117</pre><p>That observation belongs to a different boundary, so it is more useful to treat it separately. The next experiment examines what <strong>SO_RXQ_OVFL</strong> can tell us about socket-level drops under receive pressure.</p><h3>Receive Pressure Can Also Produce Drops</h3><p>The timestamp experiment showed that burst traffic could increase the measured kernel-to-userspace receive interval. The same receive path also exposed a separate signal:</p><pre>[UDP] kernel receive queue drops=8117<br></pre><p>That value came from <strong>SO_RXQ_OVFL</strong>, enabled on the UDP socket:</p><pre>int enable_rxq_overflow = 1;<br><br>::setsockopt(socket_fd_.get(),<br>             SOL_SOCKET,<br>             SO_RXQ_OVFL,<br>             &amp;enable_rxq_overflow,<br>             sizeof(enable_rxq_overflow));</pre><p>When enabled, Linux can attach an unsigned 32-bit drop counter to received datagrams through ancillary data. The value represents a cumulative count of packets dropped by the socket since its creation.</p><p>It is important not to interpret this value as receive-queue depth. A value such as <strong>8117</strong> does not mean that 8117 packets were waiting in the queue when the datagram was received. It indicates that socket-level drops had occurred before this observation.</p><p>During burst traffic, the ancillary metadata began reporting nonzero drop counts. In these experiments, that provided direct evidence that the workload could produce socket-level drops rather than merely delaying userspace processing.</p><p>This added another boundary to the investigation:</p><pre>kernel-to-userspace receive interval -&gt; when userspace serviced the receive<br><br>SO_RXQ_OVFL                          -&gt; whether socket-level drops had occurred</pre><p>The two signals can appear together, but they answer different questions. A larger receive interval does not by itself prove that packets were dropped, while a nonzero <strong>SO_RXQ_OVFL</strong> count does not explain how long a successfully received datagram waited before userspace processed it.</p><p>Treating them separately preserves what each piece of evidence can actually support.</p><h3>The Kernel Socket Buffer Is Yet Another Size Boundary</h3><p>The experiments had already introduced two different kinds of receive capacity:</p><ul><li>the userspace buffer supplied through <strong>iovec</strong>;</li><li>the application payload-size policy.</li></ul><p>Receive pressure introduced a third: the kernel-managed socket receive buffer.</p><p>The runtime inspected this value with <strong>SO_RCVBUF</strong>:</p><pre>int receive_buffer_bytes = 0;<br>socklen_t option_length = sizeof(receive_buffer_bytes);<br><br>::getsockopt(socket_fd_.get(),<br>             SOL_SOCKET,<br>             SO_RCVBUF,<br>             &amp;receive_buffer_bytes,<br>             &amp;option_length);</pre><p>This value belongs to a different boundary from the 2048-byte buffer passed to <strong>recvmsg()</strong> and the application&#39;s 512-byte payload limit.</p><p>Conceptually:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tqDEV12Ks1Qu7SM5S853aQ.png" /></figure><p>All three involve capacity, but they answer different questions.</p><p><strong>SO_RCVBUF</strong> describes kernel-managed socket receive buffering. The iovec capacity determines how much of a delivered datagram userspace can accept in a particular receive operation. <strong>MAX_PAYLOAD_SIZE</strong> determines what the application considers valid after reception.</p><p>The earlier size experiments therefore had a broader consequence: even the word “buffer” was too ambiguous unless the ownership and boundary of that buffer were identified.</p><h3>A Better Mental Model for UDP Receive Investigation</h3><p>By this point, several observations that initially appeared related had separated into distinct boundaries.</p><p>The resulting conceptual model was:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HprOdnsgmIK_fUyqzlI7uw.png" /></figure><p>This is not intended to represent the internal Linux kernel call path. It is a model of the observation boundaries that became relevant during the experiments.</p><p>Each signal belongs to a specific part of that model. IPv4 fragments were visible on the network path. <strong>MSG_TRUNC</strong> described a datagram that did not fit into the userspace buffer supplied to <strong>recvmsg()</strong>. <strong>MSG_CTRUNC</strong> indicated that the ancillary-data buffer was insufficient. <strong>SO_RXQ_OVFL</strong> provided evidence of socket-level drops, while <strong>SCM_TIMESTAMPNS</strong> provided a kernel receive timestamp. After successful reception and parsing, <strong>MAX_PAYLOAD_SIZE</strong> represented a separate application-level policy.</p><p>The value of this model is not in memorizing another networking diagram. It is in attaching each observation to the boundary capable of producing it.</p><p>When several layers expose size limits, timestamps, truncation flags, and drop counters, changing a parameter before identifying that boundary can easily address the wrong problem.</p><h3>Interpret the Evidence at the Boundary That Produced It</h3><p>Across the experiments, each observation became meaningful only after it was attached to the boundary that produced it. IPv4 fragmentation belonged to the network path, <strong>MSG_TRUNC</strong> to the userspace receive boundary, <strong>MSG_CTRUNC</strong> to ancillary-data capacity, <strong>SO_RXQ_OVFL</strong> to socket-level drops, and <strong>MAX_PAYLOAD_SIZE</strong> to application validation. <strong>SO_TIMESTAMPNS</strong> added a kernel receive timestamp, while <strong>SO_RCVBUF</strong> exposed a separate capacity boundary owned by the kernel.</p><p>The recurring pattern was that similar symptoms did not imply a shared cause. Fragmentation, truncation, application rejection, delayed userspace servicing, and socket-level drops became easier to reason about only after each observation was attached to the part of the receive path capable of producing it.</p><p>That leads to a broader systems-engineering principle:</p><blockquote><strong><em>Before changing the system in response to an observation, identify the boundary that produced the evidence.</em></strong></blockquote><p>A packet can be fragmented without being truncated. A datagram can reach userspace completely and still be rejected by the application. Payload data can fit while ancillary metadata does not. A socket can report drops without revealing its current queue depth. A measured time interval can increase without identifying one specific source of latency.</p><p>The evidence becomes useful when its meaning is constrained by the boundary that produced it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f180a0f344dc" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/msg-trunc-is-not-mtu-problem-a-linux-udp-receive-path-investigation-f180a0f344dc">MSG_TRUNC Is Not MTU Problem: A Linux UDP Receive Path Investigation</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object Lifetime Is the Real Foundation of C++]]></title>
            <link>https://levelup.gitconnected.com/object-lifetime-is-the-real-foundation-of-c-ac1bf5f6080a?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac1bf5f6080a</guid>
            <category><![CDATA[systems-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Fri, 31 Jul 2026 20:08:22 GMT</pubDate>
            <atom:updated>2026-08-03T14:23:09.901Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fWjy09Q4e8tEnKBu2yYwcw.png" /></figure><h3>A Valid Address Can Still Refer to Nothing</h3><p>Many C++ bugs are described as pointer problems.</p><p>A pointer was null. A pointer was uninitialized. A reference became invalid. An address was used after the corresponding variable had gone out of scope.</p><p>These descriptions are useful, but they often stop one level too early. A pointer is only a way to reach an object. The deeper question is whether that object still exists.</p><p>Consider a function that returns a view into a local string:</p><pre>#include &lt;string&gt;<br>#include &lt;string_view&gt;<br><br>std::string_view create_message()<br>{<br>    std::string message = &quot;runtime ready&quot;;<br>    return message;<br>}</pre><p>The returned <strong>std::string_view</strong> may contain an address that looks plausible. The memory may still contain the characters “runtime ready”. A debugger may even display the expected value.</p><p>None of that makes the access valid.</p><p>The local <strong>std::string</strong> is destroyed when <strong>create_message() </strong>returns. The view does not own the string’s character storage, so it immediately becomes dangling. Reading through it is undefined behavior.</p><p>This is what makes non-owning abstractions easy to misuse. A non-owning pointer, reference, or view can access an object, but it does not control that object’s lifetime. It does not allocate the object, keep it alive, or decide when it is destroyed. Its validity depends entirely on some other object remaining alive for as long as the access is needed.</p><p>A raw pointer often has this role:</p><pre>std::string message = &quot;runtime ready&quot;;<br>const std::string* observer = &amp;message;</pre><p>Here, <strong>observer</strong> can access <strong>message</strong>, but it does not own it. Destroying <strong>observer</strong> has no effect on <strong>message</strong>, and destroying <strong>message</strong> does not automatically reset <strong>observer</strong>. If <strong>message</strong> dies first, <strong>observer</strong> remains as a pointer value but becomes dangling.</p><p><strong>std::string_view</strong> follows the same model. It stores enough information to observe a sequence of characters, typically a pointer and a length, but it does not own those characters. The program must guarantee that the original character storage remains alive and unchanged for every use of the view.</p><p>The central question in C++ is therefore not merely:</p><blockquote>What address does this pointer contain?</blockquote><p>It is:</p><blockquote>Is there still a live object at that address, and what proves that it remains valid for this access?</blockquote><p>That question sits underneath references, iterators, views, callbacks, smart pointers, containers, and resource wrappers.</p><h3>Storage Can Survive the Object It Held</h3><p>C++ separates storage from the lifetime of the object occupying that storage.</p><p>Storage is a region of memory with enough size and alignment to hold a value. An object is a typed runtime entity whose lifetime has begun within that storage.</p><p>These are related, but they are not equivalent.</p><p>C++ defines several storage durations, including automatic, static, thread, and dynamic storage duration. These categories describe how long storage remains available. They do not, by themselves, define the full lifetime of every object placed in that storage.</p><p>Memory can exist before an object is constructed in it. The same memory can later hold another object. Storage can also remain physically readable after the lifetime of the previous object has ended.</p><p>This distinction is easy to miss because ordinary local variables make storage acquisition, construction, destruction, and storage release appear to be one event:</p><pre>void process()<br>{<br>    std::string packet = &quot;data&quot;;<br>}</pre><p>Conceptually, several transitions occur:</p><ol><li>Storage suitable for a std::string becomes available.</li><li>A std::string object is initialized in that storage.</li><li>The object is used.</li><li>Its destruction begins.</li><li>The storage later becomes available for reuse.</li></ol><p>The syntax hides these transitions because the compiler manages them automatically. The language model does not merge them.</p><p>The distinction becomes more visible when storage and construction are controlled separately:</p><pre>#include &lt;cstddef&gt;<br>#include &lt;memory&gt;<br>#include &lt;string&gt;<br><br>alignas(std::string) std::byte storage[sizeof(std::string)];<br><br>auto* message = std::construct_at(<br>    reinterpret_cast&lt;std::string*&gt;(storage),<br>    &quot;runtime ready&quot;<br>);<br><br>std::destroy_at(message);</pre><p>The byte array exists before the <strong>std::string</strong> object does. At first, storage is only raw memory: large enough because of <strong>sizeof(std::string)</strong> and correctly aligned because of <strong>alignas(std::string)</strong>.</p><p>The cast:</p><pre>reinterpret_cast&lt;std::string*&gt;(storage)</pre><p>does not create a string. It only identifies the memory location where one may be constructed. std::construct_at performs the actual construction and begins the object’s lifetime:</p><pre>auto* message = std::construct_at(<br>    reinterpret_cast&lt;std::string*&gt;(storage),<br>    &quot;runtime ready&quot;<br>);</pre><p>From that point, message refers to a live <strong>std::string</strong>.</p><p>Calling <strong>std::destroy_at(message)</strong> invokes the destructor and ends that lifetime. The raw storage still exists, and message may still contain the same address, but no live std::string remains there.</p><pre>std::destroy_at(message);<br><br>std::cout &lt;&lt; *message; // Undefined behavior</pre><p>The address did not become invalid. The object did.</p><p>The essential distinction is:</p><blockquote>Storage duration describes how long memory remains available. Object lifetime describes when a particular typed object legally exists within that memory.</blockquote><h3>Construction and Destruction Define the Valid Interval</h3><p>A constructor is often introduced as a function that assigns initial values to an object. That description understates its role.</p><p>Construction establishes a valid instance of a type and the invariants that instance is expected to maintain.</p><pre>#include &lt;stdexcept&gt;<br><br>class Socket<br>{<br>public:<br>    explicit Socket(int descriptor)<br>        : descriptor_(descriptor)<br>    {<br>        if (descriptor_ &lt; 0) {<br>            throw std::invalid_argument(&quot;invalid descriptor&quot;);<br>        }<br>    }<br><br>private:<br>    int descriptor_;<br>};</pre><p>The constructor does not merely copy an integer into <strong>descriptor_</strong>. It defines the condition under which a valid <strong>Socket</strong> can be delivered to the caller.</p><p>If construction throws, no fully constructed <strong>Socket</strong> is returned. Already constructed subobjects are destroyed, but the destructor of the incomplete <strong>Socket</strong> object is not invoked as though construction had completed successfully.</p><p>Destruction performs the opposite transition. It releases the responsibilities attached to the object and brings its valid lifetime to an end.</p><pre>#include &lt;unistd.h&gt;<br><br>class FileDescriptor<br>{<br>public:<br>    explicit FileDescriptor(int fd) noexcept<br>        : fd_(fd)<br>    {<br>    }<br><br>    FileDescriptor(const FileDescriptor&amp;) = delete;<br>    FileDescriptor&amp; operator=(const FileDescriptor&amp;) = delete;<br><br>    ~FileDescriptor()<br>    {<br>        if (fd_ &gt;= 0) {<br>            ::close(fd_);<br>        }<br>    }<br><br>private:<br>    int fd_;<br>};</pre><p>Copying is disabled because two independent objects must not both believe they own the same descriptor. Move support is omitted here because ownership transfer is a separate topic.</p><p>Once destruction has completed, the former storage no longer contains a live <strong>FileDescriptor</strong> object. The integer bit pattern may still be present in memory, but that does not preserve the ownership relationship the object represented.</p><p>Construction and destruction therefore define a valid interval:</p><ul><li>construction establishes the object and its invariants,</li><li>the object may be used while those invariants hold,</li><li>destruction releases its responsibilities and ends its lifetime.</li></ul><p>This deterministic relationship later allows RAII to bind external resources to object lifetimes. A file-descriptor wrapper, for example, can acquire ownership during construction and call <strong>close()</strong> during destruction. When the wrapper’s lifetime ends, the operating-system resource is released at the same predictable boundary.</p><h3>Scope Tracks Names, Not Every Lifetime</h3><p>Scope and lifetime frequently overlap, which makes them easy to confuse.</p><p>Scope is primarily a property of names. It determines where a name can be referenced in source code.</p><p>Lifetime is a runtime property of objects. It determines when an object exists and may be accessed.</p><p>For an ordinary local variable, both boundaries often align:</p><pre>void run()<br>{<br>    std::string state = &quot;active&quot;;<br><br>    {<br>        std::string phase = &quot;receiving&quot;;<br>        // state and phase are both in scope<br>    }<br><br>    // state is still alive<br>    // phase is no longer alive or in scope<br>}</pre><p>When the inner block ends, the name <strong>phase</strong> leaves scope and the corresponding object is destroyed. This regularity encourages the assumption that scope and lifetime are the same concept.</p><p>They are not.</p><p>An object can remain alive after the scope in which its original owner was declared has ended, provided that ownership is transferred to an object with a longer lifetime.</p><pre>#include &lt;memory&gt;<br><br>std::unique_ptr&lt;int&gt; value;<br><br>{<br>    auto local = std::make_unique&lt;int&gt;(42);<br>    value = std::move(local);<br>}<br><br>// The name local is gone.<br>// The dynamically allocated int remains alive.</pre><p>The variable <strong>local</strong> no longer exists, but the separate <strong>int</strong> object containing <strong>42</strong> remains alive. Ownership of that object has moved to <strong>value</strong>.</p><p>The opposite relationship also matters. A pointer can remain in scope after the object it refers to has died:</p><pre>const int* pointer = nullptr;<br><br>{<br>    int value = 42;<br>    pointer = &amp;value;<br>}<br><br>// pointer is still in scope.<br>// value is no longer alive.<br><br>int result = *pointer; // Undefined behavior</pre><p>A pointer can remain in scope after the object it points to has been destroyed. At that point, the pointer still holds an address, but it is dangling.</p><blockquote>Every non-owning access must occur while the target object is still alive and has not been invalidated.</blockquote><p>A non-null pointer proves only that an address is present. It does not prove that a live object of the expected type still occupies that address.</p><h3>Temporaries Hide Lifetime Boundaries</h3><p>Temporary objects often have no variable name, so the point at which their lifetime begins and ends is less visible in the source code.</p><p>Consider:</p><pre>std::string message = std::string(&quot;runtime&quot;) + &quot; ready&quot;;</pre><p>The <strong>std::string(“runtime”) </strong>part creates a temporary <strong>std::string</strong> object. It has no variable name, but it still has a lifetime. It remains alive long enough for the + operation to produce the final result, and then it is destroyed.</p><p>Problems begin when a non-owning handle escapes beyond the temporary’s lifetime:</p><pre>std::string_view message = std::string(&quot;runtime ready&quot;);</pre><p>The temporary <strong>std::string</strong> is created, and <strong>message </strong>receives a view into its character storage. Once the initialization of message is complete, the temporary std::string is destroyed. The std::string_view remains alive, but it now refers to character storage whose owning object no longer exists.</p><p>The <strong>std::string_view</strong> remains alive, but the character sequence it observes does not.</p><pre>std::cout &lt;&lt; message; // Undefined behavior</pre><p>The <strong>std::string_view</strong> object remains alive, but its target does not. The failure lies in the relationship between two different lifetimes:</p><ul><li>the observer is alive,</li><li>the observed object is not.</li></ul><p>This same pattern appears with raw pointers, references, iterators, std::span, callback captures, and borrowed ranges. A non-owning abstraction can remain structurally valid after its target has ceased to exist.</p><p>A safe design must preserve the owner or transfer the value:</p><pre>std::string create_message()<br>{<br>    return &quot;runtime ready&quot;;<br>}</pre><p>Returning by value transfers the result to the caller without exposing a borrowed reference to local storage.</p><p>A non-owning view can also be safe when the owner is guaranteed to outlive the use:</p><pre>std::string message = &quot;runtime ready&quot;;<br>std::string_view view = message;<br><br>std::cout &lt;&lt; view;</pre><p>The difference is not the syntax of <strong>std::string_view</strong>. The difference is whether the source object remains alive and stable for the duration of the access.</p><h3>Invalidation Can End a Lifetime Without Ending the Owner</h3><p>Dangling references do not arise only when an entire owner is destroyed.</p><p>A container may remain alive while operations on it destroy, replace, or relocate its elements.</p><pre>#include &lt;vector&gt;<br><br>std::vector&lt;int&gt; values{10, 20, 30};<br><br>int&amp; first = values[0];<br><br>values.reserve(values.capacity() + 1);<br><br>std::cout &lt;&lt; first; // Invalid after reallocation</pre><p>Calling <strong>reserve</strong> with a capacity greater than the current capacity forces reallocation. The vector remains alive, but its elements are moved or copied into new storage. The old element objects cease to exist at their previous locations.</p><p>The reference <strong>first</strong> still exists. The original <strong>int</strong> object it referred to does not.</p><p>This is usually described as iterator or reference invalidation, but the underlying mechanism is lifetime:</p><ol><li>An element exists at one location.</li><li>A container operation causes reallocation.</li><li>A replacement element is constructed elsewhere.</li><li>The old element’s lifetime ends.</li><li>Existing references still point to the former location.</li></ol><p>Even if the replacement contains the same numerical value, it is not the same object at the same location.</p><p>Returning a pointer, reference, iterator, std::span, or std::string_view is therefore not merely returning access to data. It publishes a lifetime dependency.</p><p>A well-designed interface should make it possible to answer:</p><ul><li>Which object owns the data?</li><li>Which operations may destroy or relocate it?</li><li>How long may the caller retain the observer?</li><li>Can another thread invalidate it?</li><li>Is the dependency encoded by the interface or left implicit?</li></ul><p>When those questions have no reliable answer, the interface is structurally unsafe.</p><h3>Lifetime Extension Is Not Ownership</h3><p>C++ provides lifetime extension in specific reference-binding situations.</p><pre>const std::string&amp; message = std::string(&quot;runtime ready&quot;);</pre><p>Normally, the temporary string would be destroyed at the end of the statement. Because it is directly bound to the local <strong>const</strong> reference in this initialization context, its lifetime is extended to match the reference.</p><p>The following use is valid:</p><pre>std::cout &lt;&lt; message;</pre><p>It does not mean that every pointer, view, wrapper, or reference extends the lifetime of its source:</p><pre>std::string_view message = std::string(&quot;runtime ready&quot;);</pre><p>No lifetime extension occurs here. <strong>std::string_view</strong> is an independent object initialized from information about the temporary string. It does not own the string and does not trigger the reference-binding rule that extends the temporary’s lifetime.</p><p>The temporary is destroyed at the end of the statement.</p><p>Lifetime extension also does not propagate transitively through arbitrary function calls:</p><pre>const std::string&amp; identity(const std::string&amp; value)<br>{<br>    return value;<br>}<br><br>const std::string&amp; message =<br>    identity(std::string(&quot;runtime ready&quot;));</pre><p>The parameter may refer to the temporary during the call. Returning that reference does not create a new lifetime guarantee for the caller. The temporary is destroyed at the end of the full expression, leaving <strong>message</strong> dangling.</p><p>The practical rule is:</p><blockquote>Lifetime extension belongs to specific initialization contexts. It is not ownership, and it is not a transitive property of references.</blockquote><p>Do not assume that a non-owning object keeps another object alive. A pointer, reference, or view only provides access to an existing object; it does not extend that object’s lifetime or take responsibility for destroying it.</p><p>The lifetime of the referenced object must be guaranteed by something else: either an explicit owner that remains alive long enough, or a specific C++ language rule such as reference lifetime extension.</p><h3>Undefined Behavior Begins Where the Lifetime Guarantee Ends</h3><p>Undefined behavior is often presented as a list of forbidden operations: dereferencing invalid pointers, reading uninitialized values, accessing arrays out of bounds, or using destroyed objects.</p><p>The deeper pattern is that the program has violated assumptions required by the C++ abstract machine.</p><p>Accessing an object outside its lifetime is one such violation.</p><p>Once an object’s lifetime ends, the implementation no longer has to preserve the behavior that would have applied while that object was alive. The compiler may assume that valid programs do not perform such accesses and optimize accordingly.</p><p>This explains why lifetime bugs can appear unstable:</p><ul><li>a value may look correct in a debug build and fail under optimization,</li><li>adding logging may change the result,</li><li>a branch may disappear,</li><li>a crash may occur far from the invalid access,</li><li>a sanitizer may detect the bug while ordinary tests do not,</li><li>the bug may remain hidden until a later change in stack or heap usage causes the same storage to be reused for something else.</li></ul><p>The compiler is not required to convert every lifetime violation into an immediate crash. A crash would often be easier to diagnose.</p><p>Instead, undefined behavior removes the guarantee connecting the source code to a predictable execution model.</p><p>Consider:</p><pre>const int* create_value()<br>{<br>    int value = 42;<br>    return &amp;value;<br>}<br><br>int main()<br>{<br>    const int* pointer = create_value();<br>    return *pointer;<br>}</pre><p>A compiler may warn about returning the address of a local variable. AddressSanitizer can also detect this class of error as a stack-use-after-return under supported configurations.</p><p>A typical build command is:</p><pre>clang++ -std=c++20 -O1 -g \<br>    -fsanitize=address \<br>    -fno-omit-frame-pointer \<br>    lifetime.cpp</pre><p>The important observation is not merely that a tool reports an invalid access.</p><p>The pointer still contains an address. The failure occurs because the object formerly occupying that stack storage no longer exists.</p><p>Sanitizers provide useful evidence, but their limits also reinforce the lifetime model:</p><blockquote>A sanitizer detecting the bug is evidence of an invalid access. A sanitizer not detecting it is not proof that the lifetime relationship is valid.</blockquote><p>A test can show that the program appeared to work in a particular execution. It cannot prove that an access was valid when the object had already been destroyed.</p><h3>C++ Correctness Is Also Temporal</h3><p>Type correctness is not enough to establish correctness in C++. An expression may use the expected types and still access an object whose lifetime has ended.</p><p>A pointer can have the correct type and still refer to a dead object. A reference has no ordinary null state, but it can still dangle. An iterator can retain the correct iterator type after the operation that invalidated it. A std::string_view can be perfectly constructed while observing storage that disappears before the next use.</p><p>Code review must therefore examine not only types and values, but also the timing of every object relationship:</p><ul><li>When does the target object’s lifetime begin?</li><li>What event ends it?</li><li>Who controls that event?</li><li>Which observers may outlive the target?</li><li>Which operations relocate or replace it?</li><li>Could a thread, callback, or asynchronous operation continue using the object after the scope that owns it has ended?</li><li>Could one object be destroyed before another object that still depends on it?</li></ul><p>These questions expose problems that type signatures alone cannot reveal.</p><p>This temporal model is what later gives meaning to move semantics, smart pointers, RAII, and iterator invalidation. Each mechanism answers a different version of the same question:</p><blockquote>Who controls a lifetime, and which operations may end or transfer it?</blockquote><p>Without a clear model of object lifetime and ownership, these features appear unrelated. With that model, they become different ways of controlling, transferring, or observing lifetimes.</p><h3>The Engineering Principle</h3><p>Many failures blamed on pointers are really failures to model time.</p><p>The pointer survives. The address remains plausible. The bytes may still be present.</p><p>But the object has already ceased to exist.</p><p>The engineering rule is therefore:</p><blockquote>Every access requires proof that the target object is still alive and has not been invalidated.</blockquote><p>Constructors, destructors, references, views, iterators, and ownership types are different mechanisms for expressing that proof.</p><p>Object lifetime is the model that connects them.</p><p>That is why it is the real foundation of C++.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac1bf5f6080a" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/object-lifetime-is-the-real-foundation-of-c-ac1bf5f6080a">Object Lifetime Is the Real Foundation of C++</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding Another UDP Socket Was Easy. The Architecture Wasn’t.]]></title>
            <link>https://levelup.gitconnected.com/adding-another-udp-socket-was-easy-the-architecture-wasnt-ce406c23554d?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce406c23554d</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Fri, 24 Jul 2026 19:52:56 GMT</pubDate>
            <atom:updated>2026-07-27T15:20:34.809Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2zLMx_0_si6uOo4OcwexeA.png" /></figure><h3>A Working Design Can Still Hide the Wrong Assumption</h3><p>Software architectures can seem flexible, but this is often only because the underlying assumptions they rely on have not yet been challenged.</p><p>Early versions of <a href="https://github.com/togunchan/EdgeNetSwitch">EdgeNetSwitch</a> had a simple networking model. One UDP socket received packets from the operating system and one <strong>UdpReceiver</strong> owned that socket. The receiver attached runtime metadata to each packet before forwarding it into the processing pipeline.</p><p>Nothing about that design was inherently wrong. In fact, it accurately described the runtime that existed at the time. There was one UDP ingress path, one receiver, and one place where UDP packets entered the system. Under those conditions, the ownership boundary appeared obvious because a single receiver represented the runtime’s entire UDP ingress subsystem.</p><p>As long as those assumptions remained true, the architecture stayed remarkably simple.</p><p>The next milestone appeared equally straightforward:</p><p><strong><em>“Support another UDP endpoint.”</em></strong></p><p>From a networking perspective, this looked almost trivial. Linux has no difficulty managing multiple UDP sockets, and supporting another endpoint appeared to require little more than creating another socket, binding it to a port, and registering it with the existing event loop.</p><p>Creating another socket was never the interesting engineering problem.</p><p>The more important question emerged only after considering what the runtime would become once multiple receivers existed.</p><blockquote><strong>If the runtime contains multiple receivers, which component should own them?</strong></blockquote><p>That question has nothing to do with UDP.</p><p>It is an architectural question.</p><h3>The Pressure Appeared Outside the Networking Layer</h3><p>Adding another receiver did not immediately change the observable behavior of the runtime. Packets still entered the runtime through the same ingress boundary and continued to traverse the processing pipeline. The architectural pressure emerged not from packet reception, but from how the runtime modeled ownership as multiple receivers became part of the same system.</p><p>The pressure appeared somewhere else.</p><p>The runtime could no longer describe itself using the same architectural model.</p><p>With a single receiver, the runtime’s UDP ingress responsibilities naturally appeared to belong to that receiver. There was no meaningful distinction between <strong><em>the receiver</em></strong> and<strong> <em>the runtime’s ingress subsystem</em> </strong>because they happened to be the same thing.</p><p>That coincidence disappeared as soon as the runtime became responsible for more than one receiver.</p><p>Questions began to emerge that no individual UdpReceiver should answer.</p><ul><li>Which receivers should exist?</li><li>Who constructs them?</li><li>Who starts them?</li><li>Who shuts them down?</li></ul><p>Those responsibilities did not emerge because UDP changed.</p><p>They emerged because the runtime had become responsible for managing a collection instead of a single component.</p><p>That distinction changed the architecture far more than adding another socket ever could.</p><h3>When One Receiver Became a Collection</h3><p>One possible solution would have been to continue constructing receivers directly inside <strong>main.cpp</strong>. From a functional perspective, nothing prevented that approach. Another receiver could simply be created alongside the first one, initialized in exactly the same way, and connected to the existing processing pipeline.</p><p>The difficulty was not duplication.</p><p>The difficulty was deciding which component should become responsible for managing the collection that was beginning to emerge.</p><p>If every new receiver were created directly inside the composition root, <strong>main.cpp</strong> would gradually become responsible for understanding receiver construction, handler creation, startup sequencing, and shutdown coordination. As the runtime continued to evolve, it would contain multiple networking objects but no architectural component representing ingress as a subsystem.</p><p>That realization led to the introduction of <a href="https://github.com/togunchan/EdgeNetSwitch/blob/main/src/runtime/IngressManager.cpp">IngressManager</a>.</p><p>Its responsibility was intentionally narrow. It did not attempt to abstract UDP, hide sockets, or introduce a transport-independent ingress model. It existed for one purpose: to manage the lifecycle of a collection of UDP ingress endpoints.</p><p>Each UdpReceiver continued to perform the same core responsibility: receiving packets from a single socket and forwarding them into the runtime.</p><p>IngressManager, however, assumed the responsibilities that existed only because multiple receivers now coexisted. It constructed the receivers, retained their ownership, created their associated handlers, and coordinated their lifecycle as a single runtime subsystem.</p><p>This change did not introduce a new networking abstraction.</p><p>It acknowledged that collection-level responsibilities no longer belonged to individual receivers. They belonged to the runtime component responsible for managing the collection itself.</p><p>As a result, <strong>main.cpp </strong>no longer needed to understand how many receivers existed or how each one should be initialized. Its role returned to what a composition root should do: assemble the runtime’s major subsystems while delegating their internal lifecycle to the components that owned them.</p><p>The networking code changed very little.</p><p>The architecture became considerably more accurate.</p><h3>A Socket Is Not a Switch Port</h3><p>Resolving the ownership of multiple receivers solved one architectural problem, but it exposed another that had remained hidden while the runtime supported only a single ingress endpoint.</p><p>Every incoming packet already had an obvious identifier.</p><p>It arrived through a specific UDP socket and that socket was already bound to a unique UDP port. At first glance, that seemed sufficient. If each receiver owned one socket, then the socket itself appeared to define where the packet had entered the runtime.</p><p>That assumption worked only because the runtime contained a single ingress path.</p><p>With one receiver, the runtime had no need to distinguish between the transport endpoint that received a packet and the logical ingress port that the switching logic would eventually require. Both concepts referred to the same runtime behavior, making them appear interchangeable.</p><p>Supporting multiple ingress endpoints revealed that they were describing entirely different things.</p><p>A UDP port identifies an operating system resource. It tells Linux which socket should receive an incoming datagram.</p><p>A switch port identifies a forwarding boundary inside the runtime. It tells the switching logic where the packet entered the network topology.</p><p>Those responsibilities belong to different layers of the system.</p><p>The operating system should identify transport endpoints.</p><p>The runtime should identify forwarding endpoints.</p><p>The distinction seems subtle until the runtime begins reasoning about packets independently of the transport mechanism that delivered them. Once packets enter the processing pipeline, forwarding decisions should depend on the runtime’s logical topology rather than on operating system resources.</p><p>At that point, the packet required an identity that belonged to the runtime itself.</p><pre>if (forwarding_engine_ &amp;&amp; processedPacket.ingress_port)<br>{<br>    auto decision = forwarding_engine_-&gt;processPacket(<br>        processedPacket,<br>        *processedPacket.ingress_port,<br>        processedPacket.timestamp_ms);<br>}</pre><p>This distinction is not merely conceptual. Without a logical ingress port, packets can still be received and parsed, but the forwarding engine cannot determine where they entered the runtime. The transport endpoint is sufficient for Linux to deliver the packet, but it is insufficient for the runtime to reason about switching decisions.</p><p>Not because Linux lacked one.</p><p>Because Linux was identifying something different.</p><h3>Identity Belongs to the Runtime</h3><p>Recognizing that a UDP socket and a switch port represented different concepts immediately raised another architectural question.</p><blockquote>If the operating system should no longer define runtime ingress identity, which component should?</blockquote><p>One possibility would have been to postpone that decision until later in the processing pipeline. The runtime could receive packets first and allow forwarding components to determine where they originated.</p><p>That approach would have distributed responsibility across multiple parts of the runtime.</p><p>Every downstream component that needed to know where a packet entered the runtime would have to answer the same question for itself. Instead of establishing logical ingress identity once at the ingress boundary, the runtime would repeat that responsibility throughout the processing pipeline.</p><p>A more accurate model emerged by treating logical ingress identity as part of the packet itself.</p><p>The moment a packet crosses the boundary between the operating system and the runtime, it enters the runtime’s domain. From that point forward, every component participating in the UDP ingress pipeline should observe the same logical ingress identity.</p><p>This shifted responsibility to the ingress boundary.</p><p>The receiver’s role was no longer limited to transferring bytes from a socket into memory. It became responsible for translating transport-specific information into runtime-specific metadata before the packet entered the rest of the system.</p><pre>auto lifecycle_id = lifecycle_gen_.next();<br><br>auto packet = parsePacket(data);<br><br>packet.lifecycle_id = lifecycle_id;<br>packet.ingress_timestamp_ns = ingress_ts;<br>packet.ingress_port = switchPort_;</pre><p>The transformation itself is intentionally small. The receiver does not make forwarding decisions or interpret the runtime topology. It simply attaches the runtime metadata that downstream components will rely on, ensuring that every packet enters the processing pipeline with the same logical context.</p><p>Linux still owned sockets.</p><p>The runtime still owned forwarding.</p><p>The receiver simply became the boundary where one representation ended and the other began.</p><p>Once that boundary existed, every downstream component could reason about packets using the runtime’s own model instead of transport-specific details.</p><p>Runtime ingress identity no longer depended on Linux.</p><p>It became part of the runtime’s architecture.</p><h3>The Architecture Changed Before the Networking Did</h3><p>One of the more interesting outcomes of this evolution was how little of the networking implementation actually changed.</p><p>Each UdpReceiver continued to own a single UDP socket.</p><p>Each receiver continued to receive datagrams using the same Linux networking APIs, while readiness remained coordinated through the shared event loop.</p><p>Packets continued to enter the processing pipeline through the same runtime boundary.</p><p>From the perspective of packet reception, almost nothing had changed.</p><p>The architectural changes happened elsewhere.</p><p>The runtime gained a component responsible for managing ingress as a subsystem rather than as an individual receiver. Packets acquired a runtime-defined ingress identity at the point where they entered the system. The composition root no longer needed to understand how receivers were constructed or coordinated because those responsibilities had moved behind a dedicated ownership boundary.</p><p>None of those changes made UDP more capable.</p><p>They made the runtime’s model more accurate.</p><p>That distinction became increasingly important as the project evolved. Supporting another endpoint no longer required changes to the runtime’s overall structure. New receivers could be added without redefining ownership boundaries, runtime ingress identity, or lifecycle coordination because those concepts had already been separated from the networking layer itself.</p><p>The implementation scaled because the architecture had already evolved to describe the responsibilities that multiple ingress endpoints introduced.</p><h3>Architecture Evolves When Assumptions Stop Matching Reality</h3><p>Looking back, adding another UDP socket was never the difficult part of this milestone.</p><p>Linux already knew how to manage multiple sockets. The networking APIs already supported the behavior the runtime needed, and introducing another receiver required relatively little implementation work.</p><p>The real challenge was recognizing that the runtime had quietly outgrown the architectural model that had originally described it so well.</p><p>A single receiver could no longer represent the entire ingress subsystem.</p><p>A Linux socket could no longer represent a logical switch port.</p><p>Neither of those models became wrong because the networking code had changed. They became incomplete because the runtime itself had evolved. What once described the system accurately no longer captured the responsibilities that had emerged as multiple ingress endpoints became part of the same runtime.</p><p>This pattern extends well beyond networking.</p><p>Architectural boundaries often remain invisible while a system is small. Components naturally accumulate responsibilities because those responsibilities happen to change together. As the system grows, those relationships begin to diverge. Responsibilities that once belonged to the same component start evolving independently, revealing boundaries that had always existed but had not yet become visible.</p><p>Good architecture rarely emerges from introducing abstractions in anticipation of future requirements. More often, it emerges when the system itself provides evidence that an existing model no longer reflects reality.</p><p>Supporting multiple UDP endpoints did not require a fundamentally different networking implementation.</p><p>It required a more accurate model of ownership, identity, and responsibility inside the runtime.</p><p>The networking problem had already been solved.</p><p>The architectural problem had not.</p><p>The second UDP socket did not expose a limitation in Linux.</p><p>It exposed a limitation in the runtime’s model of itself.</p><p>Once that distinction became visible, the necessary changes were no longer about networking.</p><p>They were about allowing the architecture to describe the runtime as it had actually become, rather than as it once had been.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce406c23554d" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/adding-another-udp-socket-was-easy-the-architecture-wasnt-ce406c23554d">Adding Another UDP Socket Was Easy. The Architecture Wasn’t.</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[epoll Didn’t Make My Runtime Faster. It Made It Correct.]]></title>
            <link>https://levelup.gitconnected.com/epoll-didnt-make-my-runtime-faster-it-made-it-correct-04155c2588b6?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/04155c2588b6</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sat, 04 Jul 2026 20:09:01 GMT</pubDate>
            <atom:updated>2026-07-06T13:14:15.584Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8BJJ1s6ww67utXyyyPw5pg.png" /></figure><p><a href="https://github.com/togunchan/EdgeNetSwitch">EdgeNetSwitch </a>did not start with <strong>epoll</strong>.</p><p>The first UDP ingress path was intentionally simple: one worker thread owned one UDP socket and waited in blocking <strong>recvfrom()</strong>.</p><p>When a packet arrived, the runtime timestamped it, published it into the internal messaging path, and let the packet processor complete the lifecycle.</p><p>That model was easy to reason about, but it raised a question once latency measurements existed: was the runtime waiting efficiently, or was it simply waiting too long?</p><p>Non-blocking polling looked like the obvious experiment. It reduced periodic latency but exposed a different problem: the runtime was doing millions of empty receive attempts while no packets were arriving.</p><p>That observation changed the investigation.</p><p>The question was no longer only whether one receive model was faster than another. The real question became:</p><p>How do we stop doing work when there is no work?</p><h3>The Blocking Runtime Was Simple for a Reason</h3><p>The original UDP ingress model used blocking <strong>recvfrom()</strong>.</p><p>A worker thread owned the UDP socket. The socket waited for incoming traffic. After a successful receive, the runtime converted the received bytes into a packet, attached timing metadata, and pushed the packet into the internal processing path.</p><p>In simplified form, the early model looked like this:</p><pre>UDP socket -&gt; blocking recvfrom() -&gt; Packet creation -&gt; MessagingBus ...<br>... -&gt; PacketProcessor -&gt; PacketProcessed/PacketDropped</pre><p>For an early deterministic networking runtime, that simplicity mattered. Before optimizing wakeups, I needed packet lifecycle behavior to be observable. A faster ingress loop would not mean much if the runtime could not explain how many packets entered, how many completed, where drops happened, or whether lifecycle accounting stayed consistent under load.</p><p>The first baseline was therefore not just a throughput baseline.</p><p>It was an observability baseline: could the runtime expose enough packet-path state to compare ingress models honestly?</p><p>That shaped the instrumentation work in v1.9.2.</p><p><strong>TimeUtils</strong> exposed a monotonic timestamp source:</p><pre>std::uint64_t nowNs()</pre><p>Each successfully received packet received an ingress timestamp:</p><pre>std::uint64_t ingress_timestamp_ns;</pre><p>Latency was then measured from successful UDP ingress observation to <strong>PacketProcessed</strong> completion. Dropped paths did not participate in this latency sample.</p><p>That distinction matters. The measurement was not an OS trace. It measured what the runtime could observe from inside its own process.</p><p>That was enough for the investigation.</p><h3>The First Blocking Baseline</h3><p>The first blocking baseline used periodic UDP traffic and sustained burst traffic.</p><p>Periodic ingress used this shape:</p><pre>while true; do <br>  printf &quot;id=1;payload=test\n&quot; <br>  sleep 0.05 <br>done | nc -u 127.0.0.1 9000</pre><p>The same command shape was also run with sleep <strong>0.1</strong> and sleep <strong>0.01</strong>.</p><p>Sustained burst ingress removed the sleep:</p><pre>while true; do <br>  printf &quot;id=1;payload=test\n&quot; <br>done | nc -u 127.0.0.1 9000</pre><p>The initial blocking runtime baseline looked like this:</p><pre>+-------------------------------+-------------+---------------------------+------------------------+-------------------------------+<br>| Traffic regime                | Observed PPS| Average accepted latency  | Max accepted latency   | Correctness state             |<br>+-------------------------------+-------------+---------------------------+------------------------+-------------------------------+<br>| Low periodic ingress (0.1s)   | ~8–9        | ~447–452 µs               | ~0.75 ms               | no drops, gap=0, duplicates=0 |<br>| Moderate periodic (0.05s)     | ~14–15      | ~421–428 µs               | ~0.88 ms               | no drops, gap=0, duplicates=0 |<br>| Higher periodic (0.01s)       | ~58–60      | ~334–337 µs               | ~0.80 ms               | no drops, gap=0, duplicates=0 |<br>| Sustained burst ingress       | ~20k        | ~86–95 µs                 | ~4.7–6.5 ms            | transient gaps, duplicates=0  |<br>+-------------------------------+-------------+---------------------------+------------------------+-------------------------------+</pre><p>The periodic runs exercised the blocking ingress path without saturating the processing path. Lifecycle accounting remained stable. <strong>processing_gap </strong>stayed at zero. <strong>duplicate_events </strong>stayed at zero. No drops were observed.</p><p>The sustained burst run changed the operating pattern. The runtime had less idle time between packets, and average ingress-to-processed latency dropped. That did not mean timing variability disappeared. Burst traffic still exposed millisecond-scale maximum latency.</p><p>The useful result was more basic: the runtime could now observe its own packet path well enough to compare receive models without relying on implicit behavior.</p><p>That made the next experiment meaningful.</p><h3>Removing the Blocking Wait</h3><p>Once the blocking baseline existed, the next experiment was obvious: remove the blocking wait and see what changed.</p><p>The runtime switched from:</p><p><em>IngressMode::</em><strong><em>Blocking</em></strong> to <em>IngressMode::</em><strong><em>NonBlocking</em></strong>.</p><p>The UDP socket was configured with <strong>O_NONBLOCK</strong> through <strong>fcntl()</strong>.</p><p>In this mode,<strong> recvfrom()</strong> no longer waits for a packet. If a packet is available, the runtime receives it. If no packet is available, <strong>recvfrom() </strong>returns <strong>EAGAIN </strong>or <strong>EWOULDBLOCK</strong>.</p><p>Blocking mode waits until work exists.</p><p>Non-blocking polling keeps asking whether work exists.</p><p>I added the non-blocking mode to test whether removing the blocking wait would reduce <strong>ingress-to-processed latency</strong> under periodic traffic. If blocking receive was adding avoidable delay, the runtime needed to expose it.</p><p>The result was clear.</p><pre>+------------------------+---------------------------+---------------------------+---------------------------+---------------------------+<br>| Traffic regime         | Observed PPS              | Average accepted latency  | Max accepted latency      | Correctness state         |<br>+------------------------+---------------------------+---------------------------+---------------------------+---------------------------+<br>| Low periodic ingress   | ~8–9                      | ~52–53 µs                 | ~0.14 ms                  | no drops, gap=0, dup=0    |<br>| (sleep 0.1)            |                           |                           |                           |                           |<br>+------------------------+---------------------------+---------------------------+---------------------------+---------------------------+<br>| Moderate periodic      | ~15–16                    | ~46–47 µs                 | ~0.13 ms                  | no drops, gap=0, dup=0    |<br>| ingress (sleep 0.05)   |                           |                           |                           |                           |<br>+------------------------+---------------------------+---------------------------+---------------------------+---------------------------+<br>| Higher periodic        | ~58–60                    | ~39 µs                    | ~0.57 ms                  | no drops, gap=0, dup=0    |<br>| ingress (sleep 0.01)   |                           |                           |                           |                           |<br>+------------------------+---------------------------+---------------------------+---------------------------+---------------------------+</pre><p>Measured directly from the runtime, non-blocking polling reduced average periodic latency significantly compared with the original blocking baseline.</p><p>At this stage, it would have been easy to stop the investigation and conclude that polling was better.</p><p>That would have been the wrong conclusion.</p><h3>The Metric That Changed the Interpretation</h3><p>The non-blocking version needed one more counter: <strong>idle_polls</strong>.</p><p>This metric increased whenever the receive loop called<strong> recvfrom() </strong>and reached <strong>EAGAIN</strong> or <strong>EWOULDBLOCK</strong> without receiving a packet.</p><p>That metric changed the interpretation of the experiment.</p><p>During idle periods with no ingress traffic, <strong>idle_polls</strong> increased very quickly. Observed values included approximately 34 million, 40 million, and 55 million iterations over short windows while no packets were being processed.</p><p>That was the first sign that the lower latency number was not free. The runtime had become more responsive by continuously spending CPU on empty receive attempts.</p><p>The tradeoff became visible:</p><pre>+---------------------------+---------------------------+----------------------+---------------------------+---------------------------+<br>| Runtime mode              | Latency profile           | CPU efficiency       | Wakeup model              | Operational cost          |<br>+---------------------------+---------------------------+----------------------+---------------------------+---------------------------+<br>| Blocking recvfrom()       | higher average latency    | efficient            | blocking wakeup           | lower idle CPU usage      |<br>| Non-blocking polling      | lower average latency     | poor                 | continuous polling loop   | aggressive idle spinning  |<br>+---------------------------+---------------------------+----------------------+---------------------------+---------------------------+</pre><p>The important result was not only that polling had lower latency.</p><p>The important result was that the runtime could now show the cost of that latency.</p><p>This changed the engineering question.</p><p>The question was no longer:</p><p>Can non-blocking sockets reduce latency?</p><p>The measurements had already answered that.</p><p>The better question was:</p><p>How do we wait without going back to a narrow blocking receive loop, and without burning CPU while idle?</p><p>That question pointed toward readiness-based event handling.</p><h3>epoll Was a Runtime Boundary, Not an API Upgrade</h3><p>EdgeNetSwitch was moving toward coordinated ownership over multiple Linux event sources: UDP sockets, control sockets, wakeup descriptors, shutdown paths, and eventually more transport backends. A runtime like that should not be built around scattered blocking calls or independent polling loops.</p><p>It also needs a shutdown path that can wake the runtime deliberately, not wait for an unrelated socket operation to return.</p><p>The problem was not just waiting for UDP.</p><p>The problem was runtime coordination.</p><p>Blocking <strong>recvfrom()</strong> worked for a single receive path, but it did not provide a clean foundation for coordinating multiple file descriptors behind one dispatch mechanism. Non-blocking polling improved latency, but it did so by spending CPU aggressively when no work existed.</p><p><strong>epoll</strong> offered a third shape:</p><ol><li>wait until one or more runtime-owned file descriptors become ready</li><li>dispatch the matching handler</li><li>process bounded work</li><li>return to waiting</li></ol><p>That model fits a deterministic runtime better than a raw receive loop.</p><p>In EdgeNetSwitch v1.9.3, epoll became the boundary for readiness ownership. The runtime did not need scattered<strong> epoll_wait() </strong>calls. It needed one readiness-driven dispatch path with explicit ownership boundaries.</p><pre>Linux file descriptors -&gt; EpollManager -&gt; EpollEventLoop -&gt; Ready handlers -&gt; UDP ready handler <br>                                                                           -&gt; Control ready handler <br>                                                                           -&gt; Shutdown wakeup path</pre><p>The event loop owns readiness waiting. The manager owns registration. Handlers own event-specific behavior. The runtime owns lifecycle decisions.</p><p>That keeps <strong>epoll </strong>from leaking everywhere.</p><p>The rest of the runtime does not need to become an <strong>epoll </strong>wrapper. It needs a clean boundary where Linux readiness becomes runtime dispatch.</p><h3>Readiness Had to Drain Work, Not Just Notice It</h3><p>For the runtime, UDP readiness had to mean more than “read one packet.”</p><p>If the receive queue already contains multiple packets, returning to epoll_wait() after a single read leaves available work behind. The epoll path therefore dispatches UDP readiness into a bounded drain:</p><pre>EPOLLIN (socket ready)<br>  -&gt; UdpReadyHandler::onEvent()<br>      -&gt; UdpReceiver::processReadableEvent()<br>          -&gt; while (true): <br>              call handleReadable() <br>              if result == NoData: <br>                  break <br>              if processed_packets &gt;= MaxPacketsPerWakeup: <br>                  break</pre><p>The current UDP path drains up to:</p><pre>MaxPacketsPerWakeup = 256</pre><p>The loop exits when <strong>handleReadable()</strong> returns <strong>NoData</strong>, which corresponds to non-blocking <strong>recvfrom()</strong> reaching <strong>EAGAIN </strong>or <strong>EWOULDBLOCK</strong>.</p><p>This is not the same thing as busy polling.</p><p>Busy polling repeatedly asks the socket whether work exists even while idle.</p><p>The epoll path waits for readiness first, then drains available work until the socket reports that no data remains or the per-wakeup budget is exhausted.</p><p>That difference matters architecturally. The runtime avoids idle spinning while still avoiding a one-packet-per-wakeup design.</p><h3>The Throughput Story Had to Be Rerun</h3><p>Before comparing all three models, one earlier mistake had to be corrected.</p><p>The v1.9.2 measurements were collected with verbose packet-path logging enabled. Later testing showed that logging was large enough to move burst throughput from the low thousands of packets per second into the hundreds of thousands.</p><p>That means the older debug-mode numbers are useful historical context, but they are not a fair baseline for judging the runtime model.</p><p>So the v1.9.3 investigation reran all three ingress models under the same <strong>warn </strong>logging configuration:</p><ul><li>blocking recvfrom()</li><li>non-blocking polling recvfrom()</li><li>epoll readiness dispatch</li></ul><p>The point was not to produce a universal benchmark.</p><p>The point was to compare the models under the same runtime conditions.</p><p>The controlled rerun used Release builds, assertions enabled, sanitizers disabled, UDP target <strong>127.0.0.1:9000</strong>, and control target <strong>/tmp/edgenetswitch.sock</strong>.</p><p>The runtime configuration used <strong>warn </strong>logging and <strong>tick_ms = 100</strong>.</p><p>That setup matters because it changed the interpretation of throughput. The earlier logging-heavy run made the ingress model look more important than it really was. Under equivalent <strong>warn </strong>logging, the three models landed much closer under burst load.</p><p>That correction is part of the engineering story.</p><p>A benchmark can be technically correct and still point</p><h3>Periodic Traffic Still Favored Polling</h3><p>The periodic tests showed the clearest behavioral difference between polling and readiness.</p><p>Non-blocking polling reacted quickly when packets arrived, but it paid for that by repeatedly calling <strong>recvfrom()</strong> while no packet was ready. Epoll did not match the polling latency in these sparse periodic tests, but it removed the busy idle loop.</p><p><strong>For sleep 0.1:</strong></p><pre>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Runtime model          | Avg latency mean (ns)| Avg latency final (ns)| Peak latency (ns)    | Processing gap max   | Duplicate terminal events max | Idle polls/drain completions final | Raw PPS final  |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Blocking recvfrom()    | 214,468              | 208,341               | 731,000              | 0                    | 0                             | 0                                  | 10             |<br>| Non-blocking polling   | 57,754               | 69,632                | 1,212,334            | 0                    | 0                             | 58,075,390                         | 10             |<br>| Epoll readiness        | 196,621              | 188,148               | 730,792              | 0                    | 0                             | 114                                | 10             |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+</pre><p><strong>For sleep 0.05</strong></p><pre>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Runtime model          | Avg latency mean (ns)| Avg latency final (ns)| Peak latency (ns)    | Processing gap max   | Duplicate terminal events max | Idle polls/drain completions final | Raw PPS final  |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Blocking recvfrom()    | 226,027              | 236,598               | 1,114,292            | 0                    | 0                             | 0                                  | 18             |<br>| Non-blocking polling   | 51,700               | 50,241                | 714,458              | 0                    | 0                             | 57,672,315                         | 18             |<br>| Epoll readiness        | 189,300              | 194,433               | 730,792              | 0                    | 0                             | 216                                | 18             |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+</pre><p><strong>For sleep 0.01</strong></p><pre>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Runtime model          | Avg latency mean (ns)| Avg latency final (ns)| Peak latency (ns)    | Processing gap max   | Duplicate terminal events max | Idle polls/drain completions final | Raw PPS final  |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+<br>| Blocking recvfrom()    | 204,588              | 184,390               | 1,691,917            | 0                    | 0                             | 0                                  | 73             |<br>| Non-blocking polling   | 48,517               | 43,345                | 461,291              | 0                    | 0                             | 57,390,902                         | 82             |<br>| Epoll readiness        | 184,128              | 181,202               | 2,800,834            | 0                    | 0                             | 876                                | 71             |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+-------------------------------+------------------------------------+----------------+</pre><p>The measured result is clear: polling produced the lowest average periodic latency, but it also produced tens of millions of idle polls. Epoll did not match polling latency in sparse traffic, but it removed the busy idle loop.</p><p>My interpretation is that polling wins the sparse-latency contest by refusing to sleep. Epoll gives up that advantage, but restores a healthier runtime model: wait for readiness, then process available work.</p><p>That is why this investigation could not stop at latency.</p><h3>Burst Traffic Changed the Shape of the Workload</h3><p>Sustained burst traffic changed the workload shape.</p><p>When the socket is almost always readable, the idle behavior that hurt polling matters less. The receive path stays hot, and all three models spend most of their time processing available work.</p><p>That is why the burst table should not be read the same way as the periodic tables.</p><pre>+------------------------+----------------------+-----------------------+----------------------+----------------------+------------------------------+----------------------+--------------------------------------+------------------+------------------+<br>| Runtime model          | Avg latency mean (ns)| Avg latency final (ns)| Peak latency (ns)    | Processing gap max   | Pending terminal events max  | Queue overflow final | Idle polls/drain completions final   | Raw PPS mean     | Raw PPS final    |<br>+------------------------+----------------------+-----------------------+----------------------+----------------------+------------------------------+----------------------+--------------------------------------+------------------+------------------+<br>| Blocking recvfrom()    | 13,491               | 12,813                | 36,323,250           | 21                   | 17                           | 4                    | 0                                    | 377,807          | 378,505          |<br>| Non-blocking polling   | 13,422               | 12,387                | 23,734,167           | 18                   | 15                           | 4                    | 2,251,353                            | 358,645          | 378,692          |<br>| Epoll readiness        | 14,166               | 12,517                | 31,967,792           | 8                    | 4                            | 4                    | 435                                  | 347,861          | 364,369          |<br>+------------------------+----------------------+----------------------+-----------------------+----------------------+------------------------------+----------------------+--------------------------------------+------------------+------------------+</pre><p>Blocking <strong>recvfrom()</strong> produced the highest average raw burst rate in this single-socket run.</p><p>Epoll was not the fastest model in that table.</p><p>The useful result is narrower and more important: epoll delivered throughput results that were comparable to the other models in this test while avoiding busy polling and preserving the runtime structure needed for coordinated multi-file-descriptor behavior.</p><p>The epoll run also showed the smallest maximum <strong>processing_gap</strong> and the smallest maximum <strong>pending_terminal_events </strong>in this burst set. That does not prove epoll will always have better backlog behavior. This was one measurement family, not a universal rule. But it shows that the readiness-based path did not damage lifecycle accounting while changing the runtime wait model.</p><h3>Correctness Was Part of the Decision</h3><p>The correctness results were important because epoll changed how the runtime waits without disrupting lifecycle tracking.</p><p>Across all measurements, no duplicate terminal events were observed and periodic processing kept up with publication. Under sustained overload, all models reported explicit <strong>queue_overflow</strong> drops, as expected, drops were visible and clearly attributed.</p><p>The measurements also continued to respond through <strong>packet-stats:json</strong>.</p><p>That point matters because EdgeNetSwitch is not only a packet receiver. It is a runtime with a control plane. If the runtime becomes fast but stops explaining its state under load, the architecture has moved in the wrong direction.</p><ul><li>The measurements showed a better result:</li><li>packet ingress remained observable</li><li>lifecycle completion remained stable</li><li>overload drops were explicit</li><li>epoll stayed in the same throughput class under burst load</li><li>idle polling was removed</li></ul><p>That combination was more important than winning every isolated benchmark column.</p><h3>Why epoll Was Chosen</h3><p>A single metric would have led to the wrong choice.</p><p>Polling looked better for sparse latency. Blocking <strong>recvfrom()</strong> looked better for raw throughput. But EdgeNetSwitch needed a model that could scale beyond a single benchmark.</p><p>The runtime needed:</p><ul><li>clear packet lifecycle handling</li><li>explicit file descriptor ownership</li><li>stable shutdown behavior</li><li>readiness-based coordination</li><li>support for multiple event sources</li><li>bounded packet draining</li><li>no unnecessary CPU spinning</li></ul><p>Epoll matched these needs better than the other approaches.</p><p>The decision was not that epoll is always faster.<br>It was that epoll provides the right runtime structure:</p><ul><li>sleep when idle</li><li>wake on readiness</li><li>dispatch explicitly</li><li>drain bounded work</li><li>preserve lifecycle</li><li>handle shutdown cleanly</li></ul><p>For simple cases, blocking or polling can look better. But for a predictable and extensible runtime, epoll was the better fit.</p><h3>What This Investigation Changed</h3><p>The main takeaway was not tied to a specific system call.</p><p>It was about understanding the boundaries of the runtime and making deliberate decisions about where control should live.</p><p>Blocking ingress conserved CPU but increased latency. Non-blocking polling reduced latency but wasted CPU. Epoll removed busy polling while staying competitive under load.</p><p>The takeaway is not a benchmark ranking, but choosing a runtime model that preserves correct behavior: clear packet lifecycle, explicit drops, deterministic shutdown, and no unnecessary work when idle.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04155c2588b6" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/epoll-didnt-make-my-runtime-faster-it-made-it-correct-04155c2588b6">epoll Didn’t Make My Runtime Faster. It Made It Correct.</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The First Lie About C++ Templates: “They Compile Like Normal Classes”]]></title>
            <link>https://levelup.gitconnected.com/the-first-lie-about-c-templates-they-compile-like-normal-classes-8864fcea1a52?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/8864fcea1a52</guid>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[systems-programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Sat, 30 May 2026 12:15:10 GMT</pubDate>
            <atom:updated>2026-06-01T14:14:04.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-28r18v2nx_HFzx9aI-lqw.png" /></figure><p>A normal C++ function has a body.</p><p>A template has a recipe.</p><p>That distinction sounds small until the build system gets involved. Then it becomes the difference between a program that links cleanly and a program that compiles every file successfully but fails at the final stage with a symbol the linker cannot find.</p><p>Consider a small fixed-capacity ring buffer:</p><pre>template &lt;size_t N&gt;<br>class RingBuffer<br>{<br>public:<br>    void push(uint8_t data);<br>    uint8_t pop();<br><br>private:<br>    std::array&lt;uint8_t, N&gt; buffer_;<br>};</pre><p>At first glance, this looks like a class declaration with a parameter. It is tempting to treat it like any other class: put the declaration in a header, move the implementation to a <strong>.cpp</strong> file, compile both, and let the linker assemble the final program.</p><p>That model is wrong.</p><p><strong>RingBuffer&lt;64&gt;</strong> and <strong>RingBuffer&lt;1024&gt;</strong> are not the same type with different runtime values. They are different concrete types generated from the same template pattern. Until the compiler sees a specific use of the template, there is no actual <strong>RingBuffer&lt;N&gt;::push()</strong> function to emit.</p><blockquote>The template is not the code.<br>The instantiation is the code.</blockquote><h3>The First Lie: “Templates Are Compiled Like Normal Classes”</h3><p>With an ordinary class, this works naturally:</p><pre>class Buffer<br>{<br>public:<br>    void push(uint8_t data);<br>};<br><br>void Buffer::push(uint8_t data)<br>{<br>    // implementation<br>}</pre><p>The compiler can compile <strong>Buffer.cpp</strong> into an object file containing one concrete symbol for <strong>Buffer::push</strong>.</p><p>Templates do not behave that way.</p><p>This implementation is still not a function:</p><pre>template &lt;size_t N&gt;<br>void RingBuffer&lt;N&gt;::push(uint8_t data)<br>{<br>    // implementation<br>}</pre><p>It is a pattern for generating functions. The compiler cannot emit one final <strong>push </strong>implementation from this alone because <strong>N</strong> is not a runtime value. It influences the actual type being generated and the code produced for that type.</p><p>The compiler needs to know which concrete form is required:</p><pre>RingBuffer&lt;64&gt; rb;<br>rb.push(42);</pre><p>Only now does <strong>RingBuffer&lt;64&gt;::push(uint8_t)</strong> become a real function candidate for code generation.</p><h3>Translation Units Are the Real Boundary</h3><p>Many template problems become easier once you stop thinking about “the project” and start thinking about individual translation units.</p><p>The compiler never sees your entire application at once.</p><p>It sees one translation unit at a time.</p><p>A translation unit is roughly one <strong>.cpp</strong> file after all <strong>#include</strong> expansion has happened. Each translation unit is compiled independently into an object file. The compiler does not freely inspect every other <strong>.cpp</strong> file while compiling the current one.</p><p>That matters because template instantiation happens during compilation, not during linking.</p><p>If <strong>main.cpp</strong> contains this:</p><pre>#include &quot;RingBuffer.hpp&quot;<br><br>int main()<br>{<br>    RingBuffer&lt;128&gt; buffer;<br>    buffer.push(1);<br>}</pre><p>then the compiler compiling <strong>main.cpp</strong> must be able to instantiate <strong>RingBuffer&lt;128&gt;::push</strong>.</p><p>If the header only contains the declaration:</p><pre>template &lt;size_t N&gt;<br>class RingBuffer<br>{<br>public:<br>    void push(uint8_t data);<br>};</pre><p>but the template body lives only in <strong>RingBuffer.cpp</strong>, then <strong>main.cpp</strong> cannot instantiate the function. It sees the shape of the class, but not the code-generation recipe for <strong>push</strong>.</p><p>The result often appears later as a linker error, but the root cause is earlier: the compiler could not generate the concrete specialization in the translation unit that needed it.</p><h3>Why the Linker Cannot Generate Missing Template Code</h3><p>This part becomes easier once you separate the roles of the compiler and the linker.</p><p>The compiler generates machine code.</p><p>The linker works with code and symbols that compilation has already produced.</p><p>That distinction is the entire issue.</p><p>Consider this structure:</p><pre>// RingBuffer.hpp<br>template &lt;size_t N&gt;<br>class RingBuffer<br>{<br>public:<br>    void push(uint8_t data);<br>};<br><br>// RingBuffer.cpp<br>#include &quot;RingBuffer.hpp&quot;<br><br>template &lt;size_t N&gt;<br>void RingBuffer&lt;N&gt;::push(uint8_t data)<br>{<br>    // implementation<br>}<br><br>// main.cpp<br>#include &quot;RingBuffer.hpp&quot;<br><br>int main()<br>{<br>    RingBuffer&lt;128&gt; rb;<br>    rb.push(1);<br>}</pre><p>At first glance, this looks reasonable.</p><p><strong>RingBuffer.cpp</strong> contains the implementation. <strong>main.cpp</strong> uses the class.</p><p>That works for ordinary classes.</p><p>But templates behave differently.</p><p>When the compiler compiles <strong>main.cpp</strong>, it encounters:</p><pre>RingBuffer&lt;128&gt; rb;<br>rb.push(1);</pre><p>At that moment, the compiler must generate a real concrete function:</p><pre>RingBuffer&lt;128&gt;::push(uint8_t)</pre><p>But <strong>main.cpp</strong> cannot see the template implementation.</p><p>It only sees the declaration:</p><pre>void push(uint8_t data);</pre><p>The actual template body exists in another translation unit: <strong>RingBuffer.cpp</strong>.</p><p>Now look at the other side.</p><p>When the compiler compiles <strong>RingBuffer.cpp</strong>, it sees the template definition.</p><p>Templates are code-generation patterns.</p><p>Seeing the definition alone is not enough to produce machine code.</p><p>Nothing in RingBuffer.cpp asks for <strong>RingBuffer&lt;128&gt;</strong>, so the compiler has no reason to emit:</p><pre>RingBuffer&lt;128&gt;::push(uint8_t)</pre><p>So, the compiler still does not know which concrete versions should actually exist in the final binary.</p><p>At this point the compiler faces a simple question:</p><p>Which RingBuffer should it generate?</p><pre>RingBuffer&lt;32&gt;::push(...)<br>RingBuffer&lt;64&gt;::push(...)<br>RingBuffer&lt;128&gt;::push(...)<br>RingBuffer&lt;1024&gt;::push(...)</pre><p>Nothing inside <strong>RingBuffer.cpp </strong>answers that question.</p><p>So the compiler treats the template merely as a reusable code-generation recipe and emits no concrete specialization.</p><p>Meanwhile, <strong>main.cpp</strong> does use:</p><pre>RingBuffer&lt;128&gt;</pre><p>But <strong>main.cpp</strong> cannot generate the specialization either, because it cannot see the template body.</p><p>So both translation units end up incomplete in different ways:</p><ul><li><strong>RingBuffer.cpp</strong> has the implementation but no instantiation request</li><li><strong>main.cpp </strong>requests the instantiation but cannot see the implementation</li></ul><p>Now the linker runs.</p><p>The linker receives object files and tries to connect symbol references.</p><p><strong>main.o</strong> says “I need this symbol”:</p><pre>RingBuffer&lt;128&gt;::push(uint8_t)</pre><p>But no object file actually contains that generated function.</p><p>The linker cannot synthesize missing template instantiations from source code.</p><p>It only works with symbols that compilers already emitted.</p><p>That is why the build fails.</p><p>Not because the template definition was missing.</p><p>But because no translation unit ever generated the concrete specialization that the final binary required.</p><h3>What Header Placement Actually Solves</h3><p>Putting template implementations in headers is not just a coding style preference.</p><p>It is fundamentally about visibility.</p><p>Templates are usually placed in headers for a much simpler reason:</p><blockquote>The compiler can only instantiate code it can see.</blockquote><p>That is why template implementations are commonly placed in headers or included through .tpp files.</p><p>Headers are simply the traditional mechanism C++ uses to make template definitions visible across translation units.</p><h3>Instantiation Is Code Generation, Not Function Calling</h3><p>This is the mental model that usually makes templates finally click.</p><p>Consider this:</p><pre>RingBuffer&lt;64&gt; a;<br>RingBuffer&lt;256&gt; b;</pre><p>The compiler does not see “one RingBuffer type configured differently at runtime.”</p><p>It sees two distinct compile-time types:</p><pre>RingBuffer&lt;64&gt;<br>RingBuffer&lt;256&gt;</pre><p>And because the class contains:</p><pre>std::array&lt;uint8_t, N&gt; buffer_;</pre><p><strong>N</strong> directly participates in the binary layout of the class itself.</p><p>These are genuinely different types, and the compiler may generate different machine code for each of them.</p><p>The compiler may therefore emit separate functions such as:</p><pre>RingBuffer&lt;64&gt;::push(uint8_t)<br>RingBuffer&lt;256&gt;::push(uint8_t)</pre><p>That is why template instantiation behaves more like compile-time code generation than ordinary runtime parameterization.</p><h3>Where “inline” Actually Fits</h3><p>A common misconception is that <strong>inline</strong> means:</p><blockquote>“the compiler will inline this function.”</blockquote><p>It does not.</p><p>Whether a function is actually inlined is the compiler’s decision, not yours.</p><p>Compilers routinely ignore inline when they believe the optimization is not worthwhile. They also frequently inline functions that were never marked inline at all.</p><p>The keyword exists for a different reason entirely.</p><p>Consider this:</p><pre>inline int add(int a, int b)<br>{<br>    return a + b;<br>}</pre><p>Because the function is marked <strong>inline</strong>, the definition may safely appear in multiple translation units as long as those definitions remain equivalent under the <a href="https://en.cppreference.com/cpp/language/definition">One Definition Rule</a>.</p><p>That is why inline functions are commonly placed in headers.</p><p>And this connects directly to templates.</p><p>In C++, member functions defined inside a class definition are implicitly inline. Template definitions follow a similar practical requirement: they often need to be visible in multiple translation units without causing ordinary multiple-definition problems.</p><p>This is one of the hidden architectural foundations behind header-heavy C++.</p><p>Without that allowance, putting template definitions in headers would immediately create multiple-definition problems.</p><p>There is a second benefit.</p><p>Once the function body lives in a header, the compiler can see it while compiling the current translation unit.</p><p>That visibility makes optimizations such as inlining and constant propagation possible.</p><p>These are related, but they are not the same mechanism.</p><p>inline does not command optimization. It allows the function body to exist in headers across multiple translation units. Once the compiler can see that body, it can decide whether inlining is worth doing.</p><h3>Explicit Instantiation: Taking Back Control</h3><p>Header-only is not the only valid architecture.</p><p>When the set of required template arguments is known and bounded, explicit instantiation can be cleaner.</p><p>Example:</p><pre>// RingBuffer.hpp<br>#pragma once<br><br>#include &lt;cstddef&gt;<br>#include &lt;cstdint&gt;<br>#include &lt;array&gt;<br><br>template &lt;size_t N&gt;<br>class RingBuffer<br>{<br>public:<br>    void push(uint8_t data);<br>    uint8_t pop();<br><br>private:<br>    std::array&lt;uint8_t, N&gt; buffer_;<br>};<br><br>// RingBuffer.cpp<br>#include &quot;RingBuffer.hpp&quot;<br><br>template &lt;size_t N&gt;<br>void RingBuffer&lt;N&gt;::push(uint8_t data)<br>{<br>    // implementation<br>}<br><br>template &lt;size_t N&gt;<br>uint8_t RingBuffer&lt;N&gt;::pop()<br>{<br>    // implementation<br>}<br><br>// Explicit instantiations<br>template class RingBuffer&lt;64&gt;;<br>template class RingBuffer&lt;128&gt;;<br>template class RingBuffer&lt;256&gt;;</pre><p>Now <strong>RingBuffer.cpp </strong>deliberately emits concrete code for those capacities.</p><p>Consumers can use:</p><pre>RingBuffer&lt;128&gt; rb;</pre><p>because the final binary will contain the explicitly instantiated symbol.</p><p>But this only works for the instantiations you name.</p><p>If another translation unit uses:</p><pre>RingBuffer&lt;1024&gt; rb;</pre><p>and no explicit instantiation exists for <strong>RingBuffer&lt;1024&gt;</strong>, the linker has the same problem again.</p><p>Explicit instantiation is useful when you already know which template versions your code is supposed to support.</p><p>Instead of allowing every translation unit to generate whatever specialization it needs, you decide up front which ones should exist.</p><p>This can reduce compile times and give you tighter control over the code your library generates.</p><h3>Header-Only Libraries Are Not Just Style</h3><p>Many modern C++ libraries are header-heavy because their abstractions depend on user-supplied types.</p><p>The STL is the obvious example. A library vendor can provide std::vector, but it cannot precompile every possible std::vector&lt;T&gt; that users may need.</p><p>The implementation must therefore remain visible so the compiler can generate the required specializations when they are needed.</p><p>A standard library vendor cannot realistically precompile every possible variation:</p><pre>std::vector&lt;MyPacket&gt;<br>std::unordered_map&lt;MyKey, MyValue, MyHasher&gt;</pre><p>Because user-defined types are not known in advance, the implementation must remain visible so the compiler can generate the required specializations when they are needed.</p><p>This is one of the reasons header-only libraries are so common in modern C++.</p><h3>The Cost: Compile Time, Binary Size, and Build Noise</h3><p>Template visibility buys flexibility, but it is not free.</p><p>Every translation unit that includes a heavy template header must parse it and potentially instantiate parts of it. That increases build times and can make large codebases harder to maintain.</p><p>Templates can also increase binary size. A function instantiated for many different types may produce multiple versions of the same algorithm:</p><pre>process&lt;int&gt;();<br>process&lt;float&gt;();<br>process&lt;double&gt;();<br>process&lt;MyPacket&gt;();<br>process&lt;MyControlMessage&gt;();</pre><p>Sometimes the optimizer can merge equivalent code. Sometimes it cannot.</p><p>This is the fundamental tradeoff. Templates move decisions from runtime to compile time, often improving performance and optimization opportunities, but the cost is paid in build complexity and binary growth.</p><blockquote>Templates are powerful.<br>The challenge is knowing when their benefits outweigh their cost.</blockquote><h3>ABI Is Where Templates Become a Library Design Problem</h3><p>Templates also complicate <a href="https://share.google/aimode/Rjv5T48fryyvoBDLh">ABI</a> boundaries.</p><p>A normal compiled library can expose a stable function:</p><pre>void process_packet(PacketView packet);</pre><p>The code inside the library can change over time. As long as the library still exposes the same binary interface, applications using it can continue working without recompilation.</p><p>A template usually exposes implementation in the header:</p><pre>template &lt;typename Packet&gt;<br>void process_packet(Packet&amp;&amp; packet)<br>{<br>    // implementation visible to users<br>}</pre><p>That creates a different relationship between the library and its users.</p><p>With a normal compiled function, users only see the interface. The implementation remains inside the library binary.</p><p>With a template, the implementation is part of the header. Users see it, compile it, and instantiate it as part of their own build.</p><p>Because the implementation is visible in the header, users compile and instantiate part of the library as part of their own build.</p><p>Even seemingly small template changes can affect source compatibility.</p><p>This is one reason production libraries often mix approaches:</p><ul><li>templates for high-performance generic frontends</li><li>non-template compiled backends for ABI stability</li><li>explicit instantiation for common supported types</li><li>type erasure or pimpl boundaries where binary compatibility matters</li></ul><p>Header-only architecture is powerful, but it weakens the separation between library author and library consumer.</p><p>The implementation becomes part of the interface in practice, even when not intended that way.</p><h3>The Real Lesson</h3><p>The common advice says templates must live in headers.</p><p>That advice is not wrong. It is incomplete.</p><p>Templates usually live in headers because the compiler can only instantiate code it can see.</p><p>Once that model clicks, template errors stop looking mysterious.</p><p>A linker error is no longer “C++ being weird.”</p><p>It is the build pipeline telling you that a concrete specialization was referenced, but no translation unit ever generated it.</p><p>That is the reality behind C++ templates.</p><p>They are not ordinary declarations waiting for linkage.</p><p>They are compile-time code-generation mechanisms living inside a language built on separate compilation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8864fcea1a52" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/the-first-lie-about-c-templates-they-compile-like-normal-classes-8864fcea1a52">The First Lie About C++ Templates: “They Compile Like Normal Classes”</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[File Descriptors Are Not Just Integers]]></title>
            <link>https://medium.com/@togunchan/file-descriptors-are-not-just-integers-ee08fb57d1e2?source=rss-767f3633b4ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee08fb57d1e2</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[systems-thinking]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Murat Toğunçhan Düzgün]]></dc:creator>
            <pubDate>Mon, 25 May 2026 16:59:57 GMT</pubDate>
            <atom:updated>2026-07-26T14:08:29.726Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*31yuJ7iL64abZY4TuiFrZA.png" /></figure><h3>The Runtime Was Already Working</h3><p>The sockets already worked. UDP ingress worked. The UNIX control socket worked. Shutdown worked.</p><p>At that stage, <a href="https://github.com/togunchan/EdgeNetSwitch">EdgeNetSwitch</a> only owned a very small number of long-lived file descriptors. Technically, the runtime did not have a descriptor bug. It was developing descriptor semantics.</p><p>That distinction ended up mattering far more than the descriptor count itself. Once a runtime starts caring about deterministic teardown, runtime observability, ownership boundaries, lifecycle correctness, and operational validation, a POSIX file descriptor stops behaving like “just an int.”</p><p>The integer is only the kernel handle. The harder problem becomes ownership.</p><h3>Linux Never Treated Them As “Just Integers”</h3><p>The common argument is technically correct:</p><pre>int fd;</pre><p>A descriptor is numerically represented as an integer. But Linux itself does not treat descriptors as primitive values.</p><p>The kernel treats them as handles into process-owned kernel resources:</p><ul><li>sockets</li><li>pipes</li><li>eventfds</li><li>files</li><li>device interfaces</li></ul><p>Each descriptor participates in lifecycle semantics: allocation, ownership, transfer, release, and invalidation.</p><p>The integer itself is only the lookup token. The operational meaning exists outside the integer.</p><p>That distinction becomes increasingly important once runtime components begin depending on descriptor lifecycle behavior.</p><h3>The Socket Wasn’t The Problem</h3><p>The sockets themselves were stable, and the runtime remained relatively small. There were no large-scale descriptor tables, no thousands of concurrent sockets, and no hyperscale infrastructure story.</p><p>What changed was the architectural expectation surrounding ownership.</p><p>Initially, the descriptor was mostly local state:</p><pre>int socket_fd = ::socket(AF_UNIX, SOCK_STREAM, 0);</pre><p>Later:</p><pre>::close(socket_fd);</pre><p>At small scale, this feels harmless. One socket. One owner. One shutdown path.</p><p>But the runtime gradually evolved additional responsibilities:</p><ul><li>ingress ownership</li><li>control-plane ownership</li><li>runtime inspection</li><li>shutdown coordination</li><li>lifecycle visibility</li></ul><p>The descriptor itself did not change. What changed was the number of runtime concerns now depending on its lifecycle semantics.</p><p>The integer was still simple. The ownership assumptions were not.</p><h3>Implementation Correctness Was No Longer Enough</h3><p>The runtime already behaved correctly. Packets flowed. Sockets opened. Sockets closed.</p><p>The issue was not implementation failure. The issue was that the runtime had started accumulating ownership semantics that were no longer explicitly modeled.</p><p>That distinction matters in long-running runtime systems, especially once resources begin surviving across subsystem boundaries.</p><p>Consider something as simple as this:</p><pre>void ConfigureSocket(int fd);       // Who owns teardown?<br>void RegisterControlSocket(int fd); // Who closes this?<br>void ExportRuntimeState(int fd);    // Does visibility survive transfer?</pre><p>Nothing here explains who owns teardown authority, who is allowed to close the descriptor, whether ownership transfer is legal, whether duplication is intentional, or whether runtime visibility survives transfer.</p><p>At that point, the descriptor stops being “local implementation detail.” It starts becoming runtime state.</p><h3>Why Shared Ownership Was Intentionally Avoided</h3><p>The obvious modern C++ reaction is usually:</p><blockquote>“Wrap it in a smart pointer.”</blockquote><p>That direction was intentionally rejected. Not because RAII was wrong but because descriptor ownership semantics mattered more than automatic cleanup.</p><p>A runtime-owned descriptor is usually controlled by one specific subsystem. That subsystem decides when the descriptor is valid, when it should be closed, and whether ownership may move somewhere else.</p><p><strong>std::shared_ptr&lt;int&gt;</strong> represents a different ownership model. It assumes that multiple parts of the system jointly own the same resource and collectively influence its lifetime.</p><p>That was not how EdgeNetSwitch wanted descriptor ownership to behave.</p><p>Using <strong>std::shared_ptr&lt;int&gt; </strong>would blur ownership boundaries in ways the runtime did not want:</p><ul><li>multiple subsystems appearing to share teardown responsibility</li><li>destruction timing depending on reference-count behavior</li><li>descriptor lifetime becoming harder to reason about during shutdown</li><li>ownership authority becoming less explicit</li></ul><p>None of those matched the runtime model.</p><p>EdgeNetSwitch instead prioritized explicit ownership, deterministic teardown, observable lifecycle state, and predictable shutdown behavior.</p><p>The runtime needed one authoritative owner. Not collaborative ownership.</p><h3>Why std::unique_ptr Still Wasn’t The Right Abstraction</h3><p>At first glance, <strong>std::unique_ptr&lt;int&gt;</strong> looks much closer to the runtime’s ownership model.</p><p>Single owner. Move-only semantics. Deterministic destruction.</p><p>Architecturally, those properties were correct.</p><p>The problem was deeper:</p><blockquote>a file descriptor is not a heap-owned integer.</blockquote><p><strong>std::unique_ptr</strong> models ownership of dynamically allocated memory:</p><pre>std::unique_ptr&lt;int&gt;</pre><p>A POSIX file descriptor is fundamentally different:</p><ul><li>a kernel-managed process resource</li><li>cleanup through <strong>::close(fd)</strong></li><li>semantic invalid state (<strong>-1</strong>)</li><li>operational lifecycle semantics</li></ul><p>Even with a custom deleter:</p><pre>std::unique_ptr&lt;int, FdCloser&gt;</pre><p>the abstraction still felt unnatural.</p><p>The runtime would first allocate heap memory, then store the descriptor inside that allocation, and finally customize destruction to call <strong>::close(fd) </strong>instead of <strong>delete</strong>.</p><p>Technically, this is entirely valid.</p><p>But the ownership model becomes indirect:</p><ul><li><strong>unique_ptr</strong> owns heap memory</li><li>the heap memory contains an integer</li><li>the integer refers to a kernel resource</li></ul><p>The runtime no longer models descriptor semantics directly.</p><p>It models pointer semantics around a descriptor value.</p><p>That distinction eventually became important.</p><p>The runtime needed explicit descriptor behavior:</p><ul><li><strong>relese()</strong></li><li>lifecycle-state transitions</li><li>shutdown visibility</li><li>runtime registration hooks</li><li>descriptor-specific invariants</li></ul><p>At that point, a dedicated type became the simpler and more accurate abstraction:</p><pre>class FileDescriptor<br>{<br>private:<br>    int fd_{-1};<br>};</pre><p>The runtime needed descriptor behavior, not pointer behavior.</p><h3>The Runtime Needed Explicit Descriptor Semantics</h3><p>Eventually, the runtime evolved toward a move-only descriptor abstraction:</p><pre>    class FileDescriptor<br>    {<br>    public:<br>        FileDescriptor() noexcept = default;<br>        FileDescriptor(int fd) noexcept;<br>        FileDescriptor(int fd, FdRegistry *registry, FdType fdType) noexcept;<br>        ~FileDescriptor();<br><br>        // Prevent copying: multiple objects must not own the same FD.<br>        FileDescriptor(const FileDescriptor &amp;) = delete;<br><br>        // Prevent assignment copying: avoids FD leaks and double-close bugs.<br>        FileDescriptor &amp;operator=(const FileDescriptor &amp;) = delete;<br><br>        FileDescriptor(FileDescriptor &amp;&amp;other) noexcept;<br>        FileDescriptor &amp;operator=(FileDescriptor &amp;&amp;other) noexcept;<br><br>        [[nodiscard]] int get() const noexcept;<br>        [[nodiscard]] bool valid() const noexcept;<br><br>        int release() noexcept;<br>        void reset(int fd = -1, FdType type = FdType::Unknown) noexcept;<br><br>    private:<br>        int fd_{-1};<br>        FdRegistry *registry_{nullptr};<br>        FdType fd_type_{FdType::Unknown};<br>    };</pre><p>The deleted copy semantics were not stylistic. They encoded ownership law.</p><p>A runtime-owned descriptor could not accidentally become multi-owned through copy propagation.</p><p>“Move-only” means ownership can be transferred, but never duplicated.</p><p>Move semantics, however, represented legitimate ownership transfer. Ownership transfer is not the same thing as ownership duplication.</p><p>The runtime intentionally allowed one while forbidding the other.</p><h3>Tracking Resources Is Not The Same Thing As Owning Them</h3><p>This eventually led to one of the most important architectural boundaries in the runtime:</p><blockquote>tracking resources is not the same thing as owning resources.</blockquote><p>EdgeNetSwitch introduced an observational <strong>FdRegistry</strong>. Critically, the registry does not own descriptors. It tracks them.</p><p>That distinction is foundational.</p><p>The registry exists for runtime inspection, lifecycle visibility, shutdown validation, and operational diagnostics. Not ownership authority.</p><p>The move-only <strong>FileDescriptor</strong> retains teardown authority over the descriptor lifecycle inside the runtime.</p><p>This separation turned out to be important architecturally. Once observability infrastructure starts owning runtime resources, lifecycle boundaries become blurry very quickly.</p><p>The registry cannot:</p><ul><li>extend descriptor lifetime</li><li>prevent destruction</li><li>authorize closure</li><li>transfer ownership</li></ul><p>It only records lifecycle state transitions. The ownership authority remains elsewhere.</p><h3>Runtime Visibility Changed The Meaning Of Shutdown</h3><p>Once descriptor lifecycle state became runtime-visible, shutdown semantics became easier to reason about operationally.</p><p>Descriptors eventually gained observable states:</p><ul><li>active</li><li>released</li><li>closed</li></ul><p>Those states were not added for cosmetic observability. They existed because the runtime needed explicit lifecycle visibility.</p><p>An <strong>active</strong> descriptor means runtime ownership still exists.</p><p>A <strong>released</strong> descriptor means ownership intentionally left runtime control boundaries.</p><p>A <strong>closed</strong> descriptor means teardown completed successfully.</p><p>This made shutdown validation much more explicit.</p><p>The runtime could now answer questions that raw integers could not:</p><ul><li>Which descriptors are still active?</li><li>Which descriptors were intentionally released?</li><li>Which descriptors reached terminal closure state?</li><li>Did shutdown complete with unresolved runtime-owned descriptors?</li></ul><p>The runtime could also expose descriptor state directly:</p><pre>{<br>  &quot;fd&quot;: 4,<br>  &quot;type&quot;: &quot;unix-control&quot;,<br>  &quot;state&quot;: &quot;active&quot;<br>}</pre><p>The descriptor itself was never the hard part. The lifecycle visibility was.</p><h3>The Descriptor Was Never “Just an Int”</h3><p>As EdgeNetSwitch evolved, file descriptors gradually stopped behaving like disposable implementation details.</p><p>The runtime was no longer only opening sockets and calling <strong>::close(fd)</strong>.</p><p>It had started modeling ownership, lifecycle transitions, teardown responsibility, and runtime visibility explicitly.</p><p>The runtime itself remained relatively small.</p><p>But the architectural pressure was already real.</p><p>And that pressure had very little to do with the integer itself.</p><p>The descriptor was never “just an int.”</p><p>The integer was only the visible handle to a much larger lifecycle model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee08fb57d1e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>