Boost C++ Libraries: Ticket #3900: Request for support of "release" member function or assign without ownership transfer to asio socket https://svn.boost.org/trac10/ticket/3900 <p> boost::asio socket has assign member function allowing to transfer native socket to the asio socket. However, there is no option to release such handler or assign without ownership transfer. </p> <p> Please add one of these options. </p> <p> Rationale: null_buffer allow easy integration of 3rd part libraries to ASIO main loop for reactor style operations. However, today it is impossible to use it with 3rd part libraries that do not transfer on the socket ownership. </p> <p> Let's assume I have 3rd part library that gives me a file descriptor to put it in select loop for asynchronous operations. </p> <p> If I assign file descriptor to the socket, I would not be able to destroy socket object because it would close the connection. and I accidentally may close other valid socket. </p> <pre class="wiki"> Thread A -------- foo f=foo_open() // fd=4 sock.assign(foo_native(f)); ... foo_close(f); // fd = 4 closed Thread B --------- some_fd = open() // some_fd = 4 Thread A -------- ~sock() /// fd=4 closed!!!! thread B can't work </pre><p> Can you please provide one (or both) of the following APIs: </p> <pre class="wiki"> // Transfer socket ownership to user and reset socket object. native_type release(); // Attach an existing native socket to the socket // (assign do not transfer ownership) void attach(const protocol_type &amp; protocol, const native_type &amp; native_socket); boost::system::error_code attach( const protocol_type &amp; protocol, const native_type &amp; native_socket, boost::system::error_code &amp; ec); </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3900 Trac 1.4.3 Maxim Yanchenko <Maxim.Yanchenko@…> Fri, 26 Feb 2010 02:10:51 GMT <link>https://svn.boost.org/trac10/ticket/3900#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:1</guid> <description> <p> I second this. Currently I use dup() as a workaround, but having a separate function would be more straightforward. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Thu, 18 Mar 2010 03:29:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:2</guid> <description> <p> The reason I have not included a socket.release() function is that it gives an incorrect illusion of portability. On Windows, adding a socket to an io_service irreversibly binds the socket to the underlying I/O completion port. The lack of release() models that restriction. </p> <p> A lot more thought is required to determine if there is a satisfactory design solution. I could, however, easily add a release() function to posix::stream_descriptor. Does that cover your use cases? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Thu, 18 Mar 2010 03:30:50 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/3900#comment:3 https://svn.boost.org/trac10/ticket/3900#comment:3 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.43.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket Maxim Yanchenko <Maxim.Yanchenko@…> Thu, 18 Mar 2010 03:47:00 GMT <link>https://svn.boost.org/trac10/ticket/3900#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:4</guid> <description> <p> In my particular case it's a third-party library that exposes its internal socket. I use Boost.Asio to read from this socket to a null buffer to determine if it's time to ask this library to read from the socket. The problem I have is double closing of the socket during shutdown - both library and asio try to close the socket. Currently I just dup the socket from the library before giving it to asio - but it's a workaround, I'd like to have something more straightforward, like a parameter for attach() like "don't close in dtor". </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Thu, 18 Mar 2010 04:06:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:5</guid> <description> <p> If you only target non-Windows platforms then posix::stream_descriptor could instead be used to wrap the file descriptor. Is that feasible? </p> <p> (I ask because adding release() to posix::stream_descriptor is doable.) </p> </description> <category>Ticket</category> </item> <item> <author>Maxim Yanchenko <Maxim.Yanchenko@…></author> <pubDate>Thu, 18 Mar 2010 04:27:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:6</guid> <description> <p> I need to test this approach in my code, but even if it doesn't work for me I believe it's still worth having posix::stream_descriptor::release() anyway. Let's see what the original submitter thinks. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 18 Mar 2010 07:54:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:7</guid> <description> <blockquote> <p> A lot more thought is required to determine if there is a satisfactory design solution. I could, however, easily add a release() function to posix::stream_descriptor. Does that cover your use cases? </p> </blockquote> <p> Defiantly not. Because AFAIK posix::stream_descriptor covers only Unix domain sockets and I need general TCP and UDP sockets, (and Unix as well). </p> <blockquote> <p> On Windows, adding a socket to an io_service irreversibly binds the socket to the underlying I/O completion port. </p> </blockquote> <p> However, if you use BOOST_ASIO_DISABLE_IOCP it would not be a problem. So, maybe it is possible to make release() fail in case IOCP is used and socket can't be released like cancel() under XP and below. </p> <p> 2nd question, at least all known to me Unix APIs: epoll, kqueue, /dev/poll provide and option to deassign socket from the underlying file descriptor. Shouldn't be something like that in Windows for IOCP? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Thu, 18 Mar 2010 09:20:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3900#comment:7" title="Comment 7">anonymous</a>: </p> <blockquote class="citation"> <p> Defiantly not. Because AFAIK posix::stream_descriptor covers only Unix domain sockets and I need general TCP and UDP sockets, (and Unix as well). </p> </blockquote> <p> I think you're confusing it with asio::local::stream_protocol::socket. If you're using it with null_buffers, posix::stream_descriptor basically works with anything that is selectable. That means sockets (TCP, UDP, UNIX), pipes, and a lot more. </p> <blockquote class="citation"> <p> However, if you use BOOST_ASIO_DISABLE_IOCP it would not be a problem. So, maybe it is possible to make release() fail in case IOCP is used and socket can't be released like cancel() under XP and below. </p> </blockquote> <p> As I said, there's still a portability issue. Adding release() isn't a satisfactory solution in my opinion. At least with cancel() we know that the OSes that don't support it are slowly fading away. </p> <blockquote class="citation"> <p> 2nd question, at least all known to me Unix APIs: epoll, kqueue, /dev/poll provide and option to deassign socket from the underlying file descriptor. Shouldn't be something like that in Windows for IOCP? </p> </blockquote> <p> There should be, but unfortunately there isn't. </p> </description> <category>Ticket</category> </item> <item> <author>artyomtnk@…</author> <pubDate>Thu, 18 Mar 2010 09:39:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:9</guid> <description> <blockquote> <p> I think you're confusing it with asio::local::stream_protocol::socket. If you're using it with null_buffers, posix::stream_descriptor basically works with anything that is selectable. That means sockets (TCP, UDP, UNIX), pipes, and a lot more. </p> </blockquote> <p> If so, then yes, it would be helpful and for Windows I would just have to Duplicate socket before assigning it. </p> <p> BTW: Maybe it is possible to provide assign member function I suggested before that would associate the socket but it would also tell not to close it in dtor and thus: </p> <p> If system is POSIX or Windows with disable IOCP then, do not call close(), otherwise, when using IOCP, attach() would duplicate the socket befor calling to assign. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Wed, 23 Feb 2011 01:06:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3900#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:10</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/69194" title="Changes for asio version 1.5.0: * Added support for timeouts on ...">[69194]</a>) Changes for asio version 1.5.0: </p> <ul><li>Added support for timeouts on socket iostreams, such as ip::tcp::iostream. A timeout is set by calling expires_at() or expires_from_now() to establish a deadline. Any socket operations which occur past the deadline will put the iostream into a bad state. </li></ul><ul><li>Added a new error() member function to socket iostreams, for retrieving the error code from the most recent system call. </li></ul><ul><li>Added a new basic_deadline_timer::cancel_one() function. This function lets you cancel a single waiting handler on a timer. Handlers are cancelled in FIFO order. </li></ul><ul><li>Added a new transfer_exactly() completion condition. This can be used to send or receive a specified number of bytes even if the total size of the buffer (or buffer sequence) is larger. </li></ul><ul><li>Added new free functions connect() and async_connect(). These operations try each endpoint in a list until the socket is successfully connected. </li></ul><ul><li>Extended the buffer_size() function so that it works for buffer sequences in addition to individual buffers. </li></ul><ul><li>Added a new buffer_copy() function that can be used to copy the raw bytes between individual buffers and buffer sequences. </li></ul><ul><li>Added new non-throwing overloads of read(), read_at(), write() and write_at() that do not require a completion condition. </li></ul><ul><li>Added friendlier compiler errors for when a completion handler does not meet the necessary type requirements. When C++0x is available (currently supported for g++ 4.5 or later, and MSVC 10), static_assert is also used to generate an informative error message. Checking may be disabled by defining BOOST_ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS. </li></ul><ul><li>Made the is_loopback(), is_unspecified() and is_multicast() functions consistently available across the ip::address, ip::address_v4 and ip::address_v6 classes. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3939" title="#3939: Feature Requests: Determination on wither ip::address is a multicast address (closed: fixed)">#3939</a>. </li></ul><ul><li>Added new non_blocking() functions for managing the non-blocking behaviour of a socket or descriptor. The io_control() commands named non_blocking_io are now deprecated in favour of these new functions. </li></ul><ul><li>Added new native_non_blocking() functions for managing the non-blocking mode of the underlying socket or descriptor. These functions are intended to allow the encapsulation of arbitrary non-blocking system calls as asynchronous operations, in a way that is transparent to the user of the socket object. The functions have no effect on the behaviour of the synchronous operations of the socket or descriptor. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3307" title="#3307: Bugs: Stream descriptor blocking state set on close (closed: fixed)">#3307</a>. </li></ul><ul><li>Added the io_control() member function for socket acceptors. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3297" title="#3297: Bugs: io_control not exposed on tcp::connector in asio (closed: fixed)">#3297</a>. </li></ul><ul><li>For consistency with the C++0x standard library, deprecated the native_type typedefs in favour of native_handle_type, and the native() member functions in favour of native_handle(). </li></ul><ul><li>Added a release() member function to posix descriptors. This function releases ownership of the underlying native descriptor to the caller. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3900" title="#3900: Feature Requests: Request for support of &#34;release&#34; member function or assign without ... (closed: fixed)">#3900</a>. </li></ul><ul><li>Added support for sequenced packet sockets (SOCK_SEQPACKET). </li></ul><ul><li>Added a new io_service::stopped() function that can be used to determine whether the io_service has stopped (i.e. a reset() call is needed prior to any further calls to run(), run_one(), poll() or poll_one()). </li></ul><ul><li>Reduced the copying of handler function objects. </li></ul><ul><li>Added support for C++0x move construction to further reduce copying of handler objects. Move support is enabled when compiling in -std=c++0x mode on g++ 4.5 or higher, or when using MSVC10. </li></ul><ul><li>Removed the dependency on OS-provided macros for the well-known IPv4 and IPv6 addresses. This should eliminate the annoying "missing braces around initializer" warnings. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3741" title="#3741: Bugs: asio::ip::address_v6.hpp: Missing braces around anonymous union (closed: fixed)">#3741</a>. </li></ul><ul><li>Reduced the size of ip::basic_endpoint&lt;&gt; objects (such as ip::tcp::endpoint and ip::udp::endpoint). </li></ul><ul><li>Changed the reactor backends to assume that any descriptors or sockets added using assign() may have been dup()-ed, and so require explicit deregistration from the reactor. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4971" title="#4971: Bugs: stream_descriptor not remove descriptor from epoll_reactor (closed: fixed)">#4971</a>. </li></ul><ul><li>Changed the SSL error category to return error strings from the OpenSSL library. </li></ul><ul><li>Changed the separate compilation support such that, to use Asio's SSL capabilities, you should also include 'asio/ssl/impl/src.hpp in one source file in your program. </li></ul><ul><li>Removed the deprecated member functions named io_service(). The get_io_service() member functions should be used instead. </li></ul><ul><li>Removed the deprecated typedefs resolver_query and resolver_iterator from the ip::tcp, ip::udp and ip::icmp classes. </li></ul><ul><li>Fixed a compile error on some versions of g++ due to anonymous enums. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4883" title="#4883: Patches: epoll_reactor.hpp doesn`t compile with some versions of gcc (closed: fixed)">#4883</a>. </li></ul><ul><li>Added an explicit cast to the FIONBIO constant to int to suppress a compiler warning on some platforms. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5128" title="#5128: Bugs: Implicit constant overflow in non_blocking_io::name() (closed: fixed)">#5128</a>. </li></ul><ul><li>Fixed warnings reported by g++'s -Wshadow compiler option. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3905" title="#3905: Bugs: asio boost headers fail with g++-4.3.2 and -Wshadow -Werror (closed: fixed)">#3905</a>. </li></ul> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Mon, 06 Jun 2011 01:43:20 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3900#comment:11 https://svn.boost.org/trac10/ticket/3900#comment:11 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Applied to release branch in <a class="changeset" href="https://svn.boost.org/trac10/changeset/72428" title="Merge asio from trunk.">[72428]</a>. (N.B. posix descriptors only.) </p> Ticket anonymous Thu, 25 Aug 2011 06:11:13 GMT <link>https://svn.boost.org/trac10/ticket/3900#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:12</guid> <description> <p> Another use case for this that presumably would be portable to windows as well is having a tcp::socket object, exchanging some messages over it, and then deciding to turn it into an SSL socket (I believe the SMTP protocol does things like this). </p> <p> In this case, the operator=(basic_socket &amp;&amp; s) is ideal, however, it would be nice to have this for current generation compilers too. maybe with a signature like: </p> <pre class="wiki">basic_socket&amp; operator=(basic_socket&amp; s); </pre> </description> <category>Ticket</category> </item> <item> <author>countforall@…</author> <pubDate>Tue, 23 Apr 2013 08:35:16 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/3900#comment:13 https://svn.boost.org/trac10/ticket/3900#comment:13 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> </ul> <p> Hi, </p> <p> I feel this problem is still existing and closed without any solution. Currently i am assign native socket after duplicating the original socket(Windows). And it has been working for last few months without any issues. But found socket issues in duplication in one of the customer machine. If i remove duplication and use the original socket, it is working fine.but i would lost the connection after boost socket destruction. </p> <p> Please add a release function to remove the native socket from boost socket or add a flag to to tell the destructor not to close the native socket. </p> <p> Thanks mahe </p> Ticket chris_kohlhoff Fri, 24 May 2013 03:30:41 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3900#comment:14 https://svn.boost.org/trac10/ticket/3900#comment:14 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Sounds like your customer has a fault Layered Service Provider installed. </p> <p> I'm restoring this bug to its fixed state to record that it was delivered in a previous release. </p> Ticket anonymous Thu, 20 Nov 2014 00:09:03 GMT <link>https://svn.boost.org/trac10/ticket/3900#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3900#comment:15</guid> <description> <p> Hi, </p> <p> I am having the same issues described on this ticket. Here the current code used to create an ASIO socket: </p> <pre class="wiki">int winSockHandle (valid Winsock handle opened by a third party Windows API) tcp::socket *pAsioSocket = new tcp::socket(ioService, tcp::v4(), winSockHandle); </pre><p> I am trying to find documentation/examples on how to implement this new POSIX release function before deleting the ASIO socket pAsioSocket. </p> <p> Is there a simple example on how to use the posix::stream_descriptor release() function? </p> <p> Thank you </p> <p> NOTE: I am still learning BOOST ASIO </p> </description> <category>Ticket</category> </item> </channel> </rss>