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

Catch random crash when notifying the client #1022

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
62 changes: 33 additions & 29 deletions electrumx/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,37 +959,41 @@ async def notify(self, touched, height_changed):
'''Notify the client about changes to touched addresses (from mempool
updates or new blocks) and height.
'''
if height_changed and self.subscribe_headers:
args = (await self.subscribe_headers_result(), )
await self.send_notification('blockchain.headers.subscribe', args)

touched = touched.intersection(self.hashX_subs)
if touched or (height_changed and self.mempool_statuses):
changed = {}

for hashX in touched:
alias = self.hashX_subs.get(hashX)
if alias:
status = await self.subscription_address_status(hashX)
changed[alias] = status

# Check mempool hashXs - the status is a function of the confirmed state of
# other transactions.
mempool_statuses = self.mempool_statuses.copy()
for hashX, old_status in mempool_statuses.items():
alias = self.hashX_subs.get(hashX)
if alias:
status = await self.subscription_address_status(hashX)
if status != old_status:
try:
if height_changed and self.subscribe_headers:
args = (await self.subscribe_headers_result(), )
await self.send_notification('blockchain.headers.subscribe', args)

touched = touched.intersection(self.hashX_subs)
if touched or (height_changed and self.mempool_statuses):
changed = {}

for hashX in touched:
alias = self.hashX_subs.get(hashX)
if alias:
status = await self.subscription_address_status(hashX)
changed[alias] = status

method = 'blockchain.scripthash.subscribe'
for alias, status in changed.items():
await self.send_notification(method, (alias, status))

if changed:
es = '' if len(changed) == 1 else 'es'
self.logger.info(f'notified of {len(changed):,d} address{es}')
# Check mempool hashXs - the status is a function of the confirmed state of
# other transactions.
mempool_statuses = self.mempool_statuses.copy()
for hashX, old_status in mempool_statuses.items():
alias = self.hashX_subs.get(hashX)
if alias:
status = await self.subscription_address_status(hashX)
if status != old_status:
changed[alias] = status

method = 'blockchain.scripthash.subscribe'
for alias, status in changed.items():
await self.send_notification(method, (alias, status))

if changed:
es = '' if len(changed) == 1 else 'es'
self.logger.info(f'notified of {len(changed):,d} address{es}')
except Exception as e:
self.logger.error(f'Error: {e}')
self.connection_lost()

async def subscribe_headers_result(self):
'''The result of a header subscription or notification.'''
Expand Down