From 8f67dbb3588f0c533033a75a4ba68ab04bb52354 Mon Sep 17 00:00:00 2001 From: Lucas Alves Date: Mon, 6 Apr 2026 00:11:33 -0300 Subject: [PATCH] Add support for release() on serial ports Fixes #1182 --- include/asio/basic_serial_port.hpp | 31 +++++++++++++++++++ .../asio/detail/posix_serial_port_service.hpp | 7 +++++ 2 files changed, 38 insertions(+) diff --git a/include/asio/basic_serial_port.hpp b/include/asio/basic_serial_port.hpp index 5605c401a9..a4f5546ed4 100644 --- a/include/asio/basic_serial_port.hpp +++ b/include/asio/basic_serial_port.hpp @@ -491,6 +491,37 @@ class basic_serial_port return impl_.get_service().native_handle(impl_.get_implementation()); } + /// Release ownership of the underlying native serial port. + /* + * This function causes all outstanding asynchronous read and write + * operations to finish immediately, and the handlers for cancelled + * operations will be passed the asio::error::operation_aborted error. + * Ownership of the native serial port is then transferred to the caller. + * + * @throws asio::system_error Thrown on failure. + */ + native_handle_type release() + { + asio::error_code ec; + native_handle_type s = impl_.get_service().release( + impl_.get_implementation(), ec); + asio::detail::throw_error(ec, "release"); + return s; + } + + /// Release ownership of the underlying native serial port. + /* + * This function causes all outstanding asynchronous read and write + * operations to finish immediately, and the handlers for cancelled + * operations will be passed the asio::error::operation_aborted error. + * Ownership of the native serial port is then transferred to the caller. + * + * @param ec Set to indicate what error occurred, if any. + */ + native_handle_type release(asio::error_code& ec) + { + return impl_.get_service().release(impl_.get_implementation(), ec); + } /// Cancel all asynchronous operations associated with the serial port. /** * This function causes all outstanding asynchronous read or write operations diff --git a/include/asio/detail/posix_serial_port_service.hpp b/include/asio/detail/posix_serial_port_service.hpp index d6fa04d4f9..938429349a 100644 --- a/include/asio/detail/posix_serial_port_service.hpp +++ b/include/asio/detail/posix_serial_port_service.hpp @@ -122,6 +122,13 @@ class posix_serial_port_service : return descriptor_service_.native_handle(impl); } + // Release ownership of the native serial port representation. + native_handle_type release(implementation_type& impl, + asio::error_code& ec) + { + return descriptor_service_.release(impl, ec); + } + // Cancel all operations associated with the serial port. asio::error_code cancel(implementation_type& impl, asio::error_code& ec)