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

Channel closing safety #13

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Changelog](https://keepachangelog.com).
- Add support for passing environment variables to remote commands with
[`Base.run(::Cmd)`](@ref) ([#12]).

### Fixed

- Fixed segfaults that would occur in [`SshChannel`](@ref) when its
[`Session`](@ref) is disconnected by the remote end ([#13]).

## [v0.5.0] - 2024-08-10

### Added
Expand Down
11 changes: 8 additions & 3 deletions src/channel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Checks if the channel is open. Wrapper around
[`lib.ssh_channel_is_open()`](@ref).
"""
function Base.isopen(sshchan::SshChannel)
if isassigned(sshchan)
if isassigned(sshchan) && (isnothing(sshchan.session) || isconnected(sshchan.session))
lib.ssh_channel_is_open(sshchan.ptr) != 0
else
false
Expand Down Expand Up @@ -160,8 +160,13 @@ function Base.close(sshchan::SshChannel)
popat!(sshchan.session.channels, idx)
end

# Close the channel
if isopen(sshchan)
if !isnothing(sshchan.session) && !isconnected(sshchan.session)
# If the session has already been disconnected from C
# (e.g. because of the other side disconnecting) then that will
# already have free'd the channel, which means we only need to
# unassign the pointer.
sshchan.ptr = nothing
elseif isopen(sshchan)
# This will trigger callbacks
closewrite(sshchan)

Expand Down
4 changes: 4 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ function Base.close(client::Client)
close(op)
end

for sshchan in client.unclaimed_channels
close(sshchan)
end

close(client.session_event)
close(client.session)
wait(client.task)
Expand Down
Loading