Skip to content

Commit

Permalink
fix(issue-84): call socket.close if socket is already connected
Browse files Browse the repository at this point in the history
  • Loading branch information
iuridiniz authored and italorossi committed May 23, 2023
1 parent 169a419 commit ffeeca7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion greenswitch/esl.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,12 @@ def connect(self):
if self._outbound_connected:
return self.session_data

resp = self.send('connect')
try:
resp = self.send('connect')
except OutboundSessionHasGoneAway as e:
# cleanup before raising exception
self.stop()
raise e
self.session_data = resp.headers
self._outbound_connected = True

Expand Down
17 changes: 17 additions & 0 deletions tests/test_outbound_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ def test_outbound_connected_is_updated_during_on_hangup_event(self):
with self.assertRaises(esl.OutboundSessionHasGoneAway):
self.outbound_session.raise_if_disconnected()

def test_raising_OutboundSessionHasGoneAway_in_session_connect_will_call_sock_close(self):
# Here we are socket connected, but not freeswitch connected
# So any exception raised in session.connect should call sock.close
self.outbound_session.connected = False

# make outbound_session.send raise OutboundSessionHasGoneAway
self.outbound_session.send = mock.MagicMock(side_effect=esl.OutboundSessionHasGoneAway)

# mock sock.close
self.outbound_session.sock.close = mock.MagicMock()

# attempt to connect and assert sock.close is called
with self.assertRaises(esl.OutboundSessionHasGoneAway):
self.outbound_session.connect()

assert self.outbound_session.sock.close.called


@pytest.mark.usefixtures("outbound_session")
@pytest.mark.usefixtures("disconnect_event")
Expand Down

0 comments on commit ffeeca7

Please sign in to comment.