Skip to content

A connection should be interruptable while waiting for new messages #46

@KillingSpark

Description

@KillingSpark

This is useful for graceful shutdown of a service. When e.g. SIGTERM is sent to your service just stopping the process is pretty rough. dbus-daemon seems to handle it well enough but it would be nice to actually close the connection properly. For that the reading should be interruptable.

This could be done by using the good ol' reliable self-pipe trick and using select() to wait for data before using a blocking recvmsg. This should also provide a way to send a code so the error handling on the reading call-site knows what's what.

The read loop would look something like this:

loop {
    match conn.get_next_message() {
        Err(Error::Interrupted(code)) => {/* probably want to return, depending on the user defined code*/}
        Err(error) => {/*other error handling*/}
        Ok(msg) => {/*message handling*/}
    }
}

When creating a conn, a second (cloneable) struct should be generated, that allows to interrupt reads on the conn at any time.
This will contain a Rc<FdWrapper> for the write end of the self-pipe. All it would do is writing the code to the pipe.
FdWrapper will close the contained RawFd on drop.

const INTR_SHUTDOWN: u8 = 0xFF;
conn_interrupter.interrupt_read(INTR_SHUTDOWN).expect("Can't even send interrupts to the conn anymore :((");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions