Closed koalo closed 1 year ago
This also fixes https://github.com/rust-netlink/rtnetlink/issues/15
Reading the linked issue and the change here looks sensible 👍 Do you have any snippets that could be used as a regression test?
Good point! The following code would for example panic previously, but not after the change:
use netlink_proto::{
new_connection,
sys::{protocols::NETLINK_AUDIT},
};
use netlink_packet_audit::{
AuditMessage,
};
use tokio::time;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let (conn, _, _) = new_connection::<AuditMessage>(NETLINK_AUDIT).unwrap();
let join_handle = tokio::spawn(conn);
time::sleep(time::Duration::from_secs(2)).await;
assert!(join_handle.is_finished());
}
Good point! The following code would for example panic previously, but not after the change:
use netlink_proto::{ new_connection, sys::{protocols::NETLINK_AUDIT}, }; use netlink_packet_audit::{ AuditMessage, }; use tokio::time; #[tokio::main(flavor = "current_thread")] async fn main() { let (conn, _, _) = new_connection::<AuditMessage>(NETLINK_AUDIT).unwrap(); let join_handle = tokio::spawn(conn); time::sleep(time::Duration::from_secs(2)).await; assert!(join_handle.is_finished()); }
It would be perfect to add this to a unit test.
Added the unit test as requested. Please have a look if that matches your expectation!
Thanks @koalo. Please fix the fmt error.
Sorry! Is fixed now.
The unsolicited messages channel is used to forward unsolicited messages like multicast messages to the user.
However, if none are sent by the kernel, the connection stays active even if the rx side of the channel was already dropped. This finally leads to resource exhaustion (e.g. too many open file descriptors) if new connections are repeatedly opened.
Therefore, explicitly check if the channel is already closed and if so, take the tx side to allow for shutdown of the connection.