Skip to content

Commit

Permalink
Fix connection interrupted and resumed callbacks (#26)
Browse files Browse the repository at this point in the history
* Fix connection interrupted and resumed callbacks

* allow a small memory leak on publishing for now
  • Loading branch information
Octogonapus authored Jul 31, 2024
1 parent 0f027fe commit 9d41512
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AWSCRT"
uuid = "df31ea59-17a4-4ebd-9d69-4f45266dc2c7"
version = "0.3.3"
version = "0.4.0"

[deps]
CountDownLatches = "621fb831-fdad-4fff-93ac-1af7b7ed19e3"
Expand Down
12 changes: 8 additions & 4 deletions src/AWSMQTT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,16 @@ end

struct _OnConnectionInterruptedEvent
callback::Function
conn::MQTTConnection
error_code::Int
end

_dispatch_event(event::_OnConnectionInterruptedEvent) = Base.invokelatest(event.callback, event.error_code)
_dispatch_event(event::_OnConnectionInterruptedEvent) = Base.invokelatest(event.callback, event.conn, event.error_code)

mutable struct _OnConnectionInterruptedUserData # mutable so it is heap allocated and has a stable address
ch::Channel{Any}
callback::Function
conn::MQTTConnection
end

function _c_on_connection_interrupted(
Expand Down Expand Up @@ -232,16 +234,18 @@ end

struct _OnConnectionResumedEvent
callback::Function
conn::MQTTConnection
return_code::aws_mqtt_connect_return_code
session_present::Bool
end

_dispatch_event(event::_OnConnectionResumedEvent) =
Base.invokelatest(event.callback, event.return_code, event.session_present)
Base.invokelatest(event.callback, event.conn, event.return_code, event.session_present)

mutable struct _OnConnectionResumedUserData # mutable so it is heap allocated and has a stable address
ch::Channel{Any}
callback::Function
conn::MQTTConnection
end

function _c_on_connection_resumed(
Expand Down Expand Up @@ -404,7 +408,7 @@ function connect(

# this ud must persist until the connection is closed
on_connection_interrupted_ud, on_connection_interrupted_udp = if on_connection_interrupted !== nothing
ud = _OnConnectionInterruptedUserData(connection.events, on_connection_interrupted)
ud = _OnConnectionInterruptedUserData(connection.events, on_connection_interrupted, connection)
udp = Base.pointer_from_objref(ud)
lock(_C_IDS_LOCK) do
# TODO we leak these refs, they are never freed
Expand All @@ -418,7 +422,7 @@ function connect(

# this ud must persist until the connection is closed
on_connection_resumed_ud, on_connection_resumed_udp = if on_connection_resumed !== nothing
ud = _OnConnectionResumedUserData(connection.events, on_connection_resumed)
ud = _OnConnectionResumedUserData(connection.events, on_connection_resumed, connection)
udp = Base.pointer_from_objref(ud)
lock(_C_IDS_LOCK) do
# TODO we leak these refs, they are never freed
Expand Down
5 changes: 3 additions & 2 deletions test/mqtt_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ end
GC.gc(true)
start_bytes = Base.gc_live_bytes()
start_nids = length(AWSCRT._C_IDS)
for _ = 1:1000
n_msgs = 1000
for _ = 1:n_msgs
task, id = publish(connection, topic1, payload1, AWS_MQTT_QOS_AT_LEAST_ONCE)
@test fetch(task) == Dict(:packet_id => id)

Expand All @@ -280,7 +281,7 @@ end
end_bytes = Base.gc_live_bytes()
end_nids = length(AWSCRT._C_IDS)
@show start_bytes end_bytes start_nids end_nids
@test end_bytes start_bytes rtol = 0.01
@test ((end_bytes - start_bytes) / n_msgs) < 500 # TODO ideally we are not leaking, but 1.9 is doing something weird. will drop support when 1.10 is officially the new LTS
@test start_nids == end_nids

fetch(unsubscribe(connection, topic1)[1])
Expand Down

2 comments on commit 9d41512

@Octogonapus
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/112122

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" 9d41512cd258f683917aff7f92f307e86e2c6ea8
git push origin v0.4.0

Please sign in to comment.