Skip to content

Commit 9c931b4

Browse files
committed
Send sessions synchronously in atexit hook
1 parent 8c650a8 commit 9c931b4

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
## TBD
5+
6+
### Bug fixes
7+
8+
* Ensure sessions are sent at exit
9+
[#371](https://github.com/bugsnag/bugsnag-python/pull/371)
10+
411
## v4.6.1 (2023-12-11)
512

613
### Bug fixes

bugsnag/delivery.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def deliver(self, config, payload: Any, options={}):
6161
"""
6262
pass
6363

64-
def deliver_sessions(self, config, payload: Any):
64+
def deliver_sessions(self, config, payload: Any, options=None):
6565
"""
6666
Sends sessions to Bugsnag
6767
"""
@@ -72,10 +72,12 @@ def deliver_sessions(self, config, payload: Any):
7272
'No sessions will be sent to Bugsnag.')
7373
self.sent_session_warning = True
7474
else:
75-
options = {
76-
'endpoint': config.session_endpoint,
77-
'success': 202,
78-
}
75+
if options is None:
76+
options = {}
77+
78+
options['endpoint'] = config.session_endpoint
79+
options['success'] = 202
80+
7981
self.deliver(config, payload, options)
8082

8183
def queue_request(self, request: Callable, config, options: Dict):

bugsnag/sessiontracker.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def start_session(self):
5454
_session_info.set(new_session)
5555
self.__queue_session(start_time)
5656

57-
def send_sessions(self):
57+
def send_sessions(self, asynchronous=True):
5858
self.mutex.acquire()
5959
try:
6060
sessions = []
@@ -66,7 +66,8 @@ def send_sessions(self):
6666
self.session_counts = {}
6767
finally:
6868
self.mutex.release()
69-
self.__deliver(sessions)
69+
70+
self.__deliver(sessions, asynchronous)
7071

7172
def __start_delivery(self):
7273
if self.delivery_thread is None:
@@ -83,7 +84,8 @@ def deliver():
8384
def cleanup():
8485
if self.delivery_thread is not None:
8586
self.delivery_thread.cancel()
86-
self.send_sessions()
87+
88+
self.send_sessions(asynchronous=False)
8789

8890
atexit.register(cleanup)
8991

@@ -96,7 +98,7 @@ def __queue_session(self, start_time: str):
9698
finally:
9799
self.mutex.release()
98100

99-
def __deliver(self, sessions: List[Dict]):
101+
def __deliver(self, sessions: List[Dict], asynchronous=True):
100102
if not sessions:
101103
self.config.logger.debug("No sessions to deliver")
102104
return
@@ -132,7 +134,19 @@ def __deliver(self, sessions: List[Dict]):
132134
)
133135

134136
encoded_payload = encoder.encode(payload)
135-
self.config.delivery.deliver_sessions(self.config, encoded_payload)
137+
138+
deliver = self.config.delivery.deliver_sessions
139+
140+
if 'options' in deliver.__code__.co_varnames:
141+
deliver(
142+
self.config,
143+
encoded_payload,
144+
options={'asynchronous': asynchronous}
145+
)
146+
else:
147+
deliver(self.config, encoded_payload)
148+
149+
136150
except Exception as e:
137151
self.config.logger.exception('Sending sessions failed %s', e)
138152

0 commit comments

Comments
 (0)