rust-netlink / netlink-proto

Other
8 stars 14 forks source link

Check for closed unsolicited messages channel #10

Closed koalo closed 1 year ago

koalo commented 1 year ago

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.

koalo commented 1 year ago

This also fixes https://github.com/rust-netlink/rtnetlink/issues/15

JohnTitor commented 1 year ago

Reading the linked issue and the change here looks sensible 👍 Do you have any snippets that could be used as a regression test?

koalo commented 1 year ago

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());
}
wllenyj commented 1 year ago

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.

koalo commented 1 year ago

Added the unit test as requested. Please have a look if that matches your expectation!

koalo commented 1 year ago

Thanks @koalo. Please fix the fmt error.

Sorry! Is fixed now.