Skip to content

Add support for custom signal handler - #948

Open
feois wants to merge 13 commits into
RustAudio:masterfrom
feois:master
Open

feois wants to merge 13 commits into
RustAudio:masterfrom
feois:master

Conversation

@feois

@feois feois commented Nov 26, 2026 •

Copy link
Copy Markdown

Add SourcesQueueInput::append_with_callback that allows user to provide impl FnOnce() + Send as a callback.

@feois

feois commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

actually i wonder if returning receiver is the right choice, passing sender as argument might be better, but it'll be inconsistent with current api

@feois

feois commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Actually I just completely understand what's wrong, it should be accepting a Box<dyn FnOnce()> and it will be usable in all cases. The original API design is just flawed

wilson added 4 commits November 27, 2026 01:33
This reverts commit f95e6d7.
This reverts commit d3e4d4e.
This reverts commit a22f751.
@feois feois changed the title Add tokio support for signals Add support for custom signal handler Nov 26, 2026

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • A one-shot closure is a good fit for completion. Could we call this append_with_callback and take impl FnOnce() + Send + 'static with no return value? The current signature makes callers box the closure and creates a channel even when they only want the callback. append_with_signal already gives callers a receiver. (src/queue.rs:78, src/queue.rs:94)
  • Please document that the callback runs inline when the queue is pulled past the source's end. With device playback, this can run on the audio thread while samples are still buffered. The callback should return promptly and avoid blocking or panicking. Clearing a pending source or dropping the output before the callback runs skips it. (src/queue.rs:90, src/queue.rs:124, src/queue.rs:280, src/stream.rs:536)
  • Please add a queue test that checks the callback fires once before the next source yields. Please also use SourcesQueueInput in the changelog. (src/queue.rs:300, CHANGELOG.md:21)

@feois

feois commented Nov 27, 2026 •

Copy link
Copy Markdown
Author
  • A one-shot closure is a good fit for completion. Could we call this append_with_callback and take impl FnOnce() + Send + 'static with no return value? The current signature makes callers box the closure and creates a channel even when they only want the callback. append_with_signal already gives callers a receiver. (src/queue.rs:78, src/queue.rs:94)
  • Please document that the callback runs inline when the queue is pulled past the source's end. With device playback, this can run on the audio thread while samples are still buffered. The callback should return promptly and avoid blocking or panicking. Clearing a pending source or dropping the output before the callback runs skips it. (src/queue.rs:90, src/queue.rs:124, src/queue.rs:280, src/stream.rs:536)
  • Please add a queue test that checks the callback fires once before the next source yields. Please also use SourcesQueueInput in the changelog. (src/queue.rs:300, CHANGELOG.md:21)
  • The channel still needs to be created so Player::sleep_until_end() still works, at that point might as well return receiver if the user needs it, I don't think generic works unless we add a generic argument to the struct, so Box<dyn> is pretty much required
  • Good point
  • That was a typo

@feois

feois commented Nov 27, 2026

Copy link
Copy Markdown
Author

I believe all issues raised have been fixed, remind me if there's any issue I need to fix.
One thing, changing the argument to impl FnOnce() improves ergonomics with closures but introduces an extra indirection if users already have Box<dyn FnOnce()>, though I guess it'll not be a huge concern for most use cases.

@feois
feois requested a review from kapok-4o November 27, 2026 13:10

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick update, the rename, the FnOnce() + Send + 'static bound, and the changelog fix (src/queue.rs:96, CHANGELOG.md:21).

  • Please return () and omit channel creation (src/queue.rs:96). Player::sleep_until_end already gets its receiver through append_with_signal (src/player.rs:169).
  • Please document that polling past the source's end calls the callback inline, possibly on the audio thread while samples are still buffered. It should return promptly without blocking or panicking. Clearing the pending source or dropping the output before the call skips it (src/queue.rs:90, src/queue.rs:123, src/queue.rs:279, src/stream.rs:536).
  • The test checks a flag after polling an empty source. Please queue two nonempty sources and check that the callback fires exactly once, before the second source yields (src/queue.rs:513).

A Box<dyn FnOnce() + Send + 'static> implements FnOnce and can be passed directly. The extra allocation at enqueue time and extra indirection at completion seem fine for a one-shot callback (src/queue.rs:104, src/queue.rs:283).

Library tests pass with both channel implementations.

@feois

feois commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thanks for the quick update, the rename, the FnOnce() + Send + 'static bound, and the changelog fix (src/queue.rs:96, CHANGELOG.md:21).

  • Please return () and omit channel creation (src/queue.rs:96). Player::sleep_until_end already gets its receiver through append_with_signal (src/player.rs:169).
  • Please document that polling past the source's end calls the callback inline, possibly on the audio thread while samples are still buffered. It should return promptly without blocking or panicking. Clearing the pending source or dropping the output before the call skips it (src/queue.rs:90, src/queue.rs:123, src/queue.rs:279, src/stream.rs:536).
  • The test checks a flag after polling an empty source. Please queue two nonempty sources and check that the callback fires exactly once, before the second source yields (src/queue.rs:513).

A Box<dyn FnOnce() + Send + 'static> implements FnOnce and can be passed directly. The extra allocation at enqueue time and extra indirection at completion seem fine for a one-shot callback (src/queue.rs:104, src/queue.rs:283).

Library tests pass with both channel implementations.

Aight, should be fixed. my brain was still thinking of the old implementation which is why I thought channel must be created 😂, sorry about the brainfart

@feois
feois requested a review from kapok-4o November 27, 2026 16:17
@feois

feois commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

Actually, append_with_signal can use append_with_callback internally, unifying the implementation (and removes the need for the enum), but of course it doesn't offer any benefit for now and introduces extra indirection. I guess if in the future the method needs to be expanded with more lines it can be considered.

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is close. The return type, the inline-callback docs and the enum all look good to me. Two small things left:

  • Could the docs also say the callback is skipped if clear() removes the pending source or the queue output is dropped before it runs (src/queue.rs:99, src/queue.rs:126, src/queue.rs:135)?
  • The test calls go_next() twice and never reads a sample. Could it drive the queue with next() and check the count is zero through the first source, already one when the second source yields its first sample, and still one after the queue is drained (src/queue.rs:232, src/queue.rs:530)?

Agreed that append_with_signal can keep using the channel directly (src/queue.rs:83).

@feois

feois commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

Thanks, this is close. The return type, the inline-callback docs and the enum all look good to me. Two small things left:

  • Could the docs also say the callback is skipped if clear() removes the pending source or the queue output is dropped before it runs (src/queue.rs:99, src/queue.rs:126, src/queue.rs:135)?
  • The test calls go_next() twice and never reads a sample. Could it drive the queue with next() and check the count is zero through the first source, already one when the second source yields its first sample, and still one after the queue is drained (src/queue.rs:232, src/queue.rs:530)?

Agreed that append_with_signal can keep using the channel directly (src/queue.rs:83).

I am not sure if I understand correctly what you meant, do you want to check if the callback is called if the final sample of first source is yielded or when the first sample of second source is yielded? It seems that the current behavior is latter so I will write test for that.

@kapok-4o

Copy link
Copy Markdown

Yes, the latter: by the time next() returns the second source's first sample, the callback has run exactly once, and not before the first source is exhausted. Testing the current behavior is what I meant.

@feois

feois commented Nov 27, 2026

Copy link
Copy Markdown
Author

Yes, the latter: by the time next() returns the second source's first sample, the callback has run exactly once, and not before the first source is exhausted. Testing the current behavior is what I meant.

Done. Anything else to add?

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sticking with this. Looks good to me.

Two optional touches, not blocking:

  • The note says the callback is skipped when clear() is called, but clear() only drops pending sources, so the playing source's callback still runs (src/queue.rs:101, src/queue.rs:299). "if its source is removed by clear() before it plays" would be exact.
  • The test could assert the count is still 0 on each sample of the first source and 1 when next() returns Some(0.4), so an early callback would fail it (src/queue.rs:533).

@feois

feois commented Nov 28, 2026

Copy link
Copy Markdown
Author

Thanks for sticking with this. Looks good to me.

Two optional touches, not blocking:

  • The note says the callback is skipped when clear() is called, but clear() only drops pending sources, so the playing source's callback still runs (src/queue.rs:101, src/queue.rs:299). "if its source is removed by clear() before it plays" would be exact.
  • The test could assert the count is still 0 on each sample of the first source and 1 when next() returns Some(0.4), so an early callback would fail it (src/queue.rs:533).

Done 👍, I also added some docs to clear()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants