Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Asynchronous cancellation for ddprof_ffi_ProfileExporterV3_send#45

Merged
ivoanjo merged 14 commits into
mainfrom
ivoanjo/send-cancellation
Apr 14, 2022
Merged

Asynchronous cancellation for ddprof_ffi_ProfileExporterV3_send#45
ivoanjo merged 14 commits into
mainfrom
ivoanjo/send-cancellation

Conversation

@ivoanjo

@ivoanjo ivoanjo commented Apr 4, 2022

Copy link
Copy Markdown
Member

What does this PR do?

Add CancellationToken to ffi, and change exporter to return early from a send if CancellationToken gets marked as cancelled.

Motivation

By default, Ruby (and Java) set a profile export timeout of 30 seconds AKA in the worst case, a call to ddprof_ffi_ProfileExporterV3_send can block for that amount of time.

For Ruby in particular, we want to call ddprof_ffi_ProfileExporterV3_send from a Ruby thread, but there needs to be a way to interrupt a long-running send in case the
application wants to exit (e.g. the user pressed ctrl+c).

Without a way to interrupt a send, the Ruby VM will hang until the timeout is hit.

Additional Notes

I'm marking this PR as WIP since I need some help with the lifetime of the CancellationToken: right now it's getting dropped by the functions that use it, whereas it should only be dropped when ddprof_ffi_CancellationToken_drop manually does it.

(Fixed!)

How to test the change?

I've modified the exporter.cpp to use the cancellation mechanism: if a request takes longer than 5 seconds, it will call cancel and cause send to return earlier than the set timeout.

Comment thread ddprof-exporter/src/lib.rs Outdated

@morrisonlevi morrisonlevi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks okay, just have a comment/question about the pub struct CancellationToken.

Comment thread ddprof-ffi/src/exporter.rs Outdated
Comment thread ddprof-ffi/src/exporter.rs Outdated
ivoanjo added 3 commits April 12, 2022 13:48
By default, Ruby (and Java) set a profile export timeout of 30 seconds
AKA in the worst case, a call to `ddprof_ffi_ProfileExporterV3_send`
can block for that amount of time.

For Ruby in particular, we want to call
`ddprof_ffi_ProfileExporterV3_send` from a Ruby thread, but there
needs to be a way to interrupt a long-running `send` in case the
application wants to exit (e.g. the user pressed ctrl+c).

Without a way to interrupt a `send`, the Ruby VM will hang until
the timeout is hit.

To fix this, we make use of tokio's `CancellationToken`: this
concurrency utility is built so that it can be used to signal from
one thread to another that we want to return early.

**NOTE**: I'm marking this PR as WIP since I need some help with
the lifetime of the `CancellationToken`: right now it's getting
dropped by the functions that use it, whereas it should only be
dropped when `ddprof_ffi_CancellationToken_drop` manually does it.
@ivoanjo
ivoanjo force-pushed the ivoanjo/send-cancellation branch from c67a7a7 to 38646dd Compare April 12, 2022 14:25
@ivoanjo ivoanjo changed the title WIP: Asynchronous cancellation for ddprof_ffi_ProfileExporterV3_send Asynchronous cancellation for ddprof_ffi_ProfileExporterV3_send Apr 12, 2022
@ivoanjo
ivoanjo force-pushed the ivoanjo/send-cancellation branch from 927826a to f5ab2b7 Compare April 12, 2022 14:36
ivoanjo added 3 commits April 12, 2022 15:37
The insight needed to fix my first attempt at implementing cancellation
is that the `Box` type automatically drops whatever it was pointing to
when the current scope ends.

Thus, to leak something out of Rust's control, we need to use
`Box::into_raw(Box::new(ThingToBeLeaked()))`, which returns a
`*mut ThingToBeLeaked` aka a raw, unsafe pointer.

Then, to borrow that something for use, we can use
`unsafe { raw_pointer.as_ref() }` to get a `&ThinkToBeLeaked` back.

Finally, when we want to free that thing again, we can turn it
back into a box; here I'm doing it implicitly, but one other
way is to use `Box::from_raw`. Then `drop` can be used again,
explicitly or implicitly.
@ivoanjo
ivoanjo force-pushed the ivoanjo/send-cancellation branch from f5ab2b7 to b59f166 Compare April 12, 2022 14:37
@ivoanjo
ivoanjo force-pushed the ivoanjo/send-cancellation branch from 69e9c99 to 4981280 Compare April 12, 2022 14:39
@ivoanjo

ivoanjo commented Apr 12, 2022

Copy link
Copy Markdown
Member Author

@morrisonlevi @paullegranddc can you take a second look?

With a cooler head and a few more learnings from the past days I dug back into the Rust book and figured out how to get the correct lifetime for the CancellationToken instances. I've also updated the C++ example so that anyone can play with it as well.

Comment thread ddprof-exporter/src/lib.rs Outdated
Comment thread ddprof-exporter/src/lib.rs Outdated
Comment thread ddprof-exporter/src/lib.rs Outdated
Comment thread examples/ffi/exporter.cpp
Comment thread ddprof-ffi/src/exporter.rs
Comment on lines +318 to +324
if cancel_reference.is_cancelled() {
return false;
}
cancel_reference.cancel();

true
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a race condition, yeah? Some other thread could have cancelled between the check and the cancel. Can we call cancel even if it's been cancelled? If hope so, and if so I'd prefer that and return nothing instead of bool.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, looking at the code, cancelling twice doesn't do anything. It's just an atomically updated flag.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, this is inherently a race -- the early return is not an attempt at cancelling exactly once (the CancellationToken already does that as Paul pointed out), I'm just trying to add some visibility to the caller.

Happy to change this to not return anything, if we feel like this return is not useful.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think I would use it, but if you think you would in Ruby for some diagnostic or something go ahead.

morrisonlevi and others added 6 commits April 13, 2022 19:31
…use it

A cloned CancellationToken is connected to the CancellationToken it was
created from.
Either the cloned or the original token can be used to cancel or
provided as arguments to send.
The useful part is that they have independent lifetimes and can be
dropped separately.

Thus, it's possible to do something like:

```c
cancel_t1 = ddprof_ffi_CancellationToken_new();
cancel_t2 = ddprof_ffi_CancellationToken_clone(cancel_t1);

// On thread t1:
    ddprof_ffi_ProfileExporterV3_send(..., cancel_t1);
    ddprof_ffi_CancellationToken_drop(cancel_t1);

// On thread t2:
    ddprof_ffi_CancellationToken_cancel(cancel_t2);
    ddprof_ffi_CancellationToken_drop(cancel_t2);
```

Without clone, both t1 and t2 would need to synchronize to make sure
neither was using the cancel before it could be dropped. With clone,
there is no need for such synchronization, both threads have their
own cancel and should drop that cancel after they are done with it.
/// Without clone, both t1 and t2 would need to synchronize to make sure neither was using the cancel
/// before it could be dropped. With clone, there is no need for such synchronization, both threads
/// have their own cancel and should drop that cancel after they are done with it.
pub extern "C" fn ddprof_ffi_CancellationToken_clone(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice documentation

@ivoanjo
ivoanjo merged commit ac75ab9 into main Apr 14, 2022
@ivoanjo
ivoanjo deleted the ivoanjo/send-cancellation branch April 14, 2022 15:10
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request Apr 26, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request May 4, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request May 16, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request May 17, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request May 19, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request May 25, 2022
Up until now, `ddprof_ffi_ProfileExporterV3_send` was blocking and
nothing on the Ruby side could interrupt it.

This meant that if reporting a profile was slow/hung, then pressing
ctrl+c or calling `Thread#kill` would do nothing -- send would
only return after it hit the configured timeout (which by default is
30 seconds).

This was very annoying if we're talking about an app that is trying
to shut down and is being delayed by a report that is taking longer
than expected.

To fix this, we make use of the `CancellationToken` added in
<DataDog/libddprof#45> to enable the Ruby VM
to signal to libddprof that it should cancel an in-progress `send`.

Thus, as the added spec shows, we can make `send` return early
by interrupting the thread, rather than needing to wait until the
timeout was hit.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants