Skip to content

Commit

Permalink
Merge pull request #13 from JuliaWeb/channel-closing
Browse files Browse the repository at this point in the history
Channel closing safety
  • Loading branch information
JamesWrigley authored Sep 24, 2024
2 parents 1ca340e + 55ef5e4 commit ef719b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
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

0 comments on commit ef719b4

Please sign in to comment.