Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detach when attaching process ends #115

Closed
cjbottaro opened this issue Aug 7, 2022 · 1 comment
Closed

Automatically detach when attaching process ends #115

cjbottaro opened this issue Aug 7, 2022 · 1 comment

Comments

@cjbottaro
Copy link

I have a use case where a GenServer needs to monitor a connection. The connection automatically reconnects when it disconnects, and it publishes telemetry events for it.

The problem is my GenServer can crash and get restarted, but :telemetry doesn't clean up the event handler.

Task.async(fn -> :ok = :telemetry.attach(:foo, :foo, &handler/4, :foo) end)
|> Task.await()

:telemetry.list_handlers(:foo) != []

Is there any performant way to have :telemetry Process.monitor/1 anything that calls attach/4 and automatically detaches when those processes end?

There are ways to do this on the user side of :telemetry, but it seems very hacky: The config argument of attach could be {handler_id, pid_of_caller}, then the handler can call Process.alive?(pid_of_caller) and call detach if true. It also seems less performant than just having a process monitor.

Thanks for the help.

@josevalim
Copy link
Contributor

Monitor is the way to go. Monitor it and remove it in the DOWN message. :)

for a GenServer, you may also be fine with trapping exits and a terminate callback.

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

No branches or pull requests

2 participants