# Buggy code
def get_current_state_deltas(self, prev_stream_id):
    prev_stream_id = int(prev_stream_id)
    if not self._curr_state_delta_stream_cache.has_any_entity_changed(prev_stream_id):
        return []

    def get_current_state_deltas_txn(txn):
        # First we calculate the max stream id that will give us less than
        # N results.
        # We arbitarily limit to 100 stream_id entries to ensure we don't
        # select toooo many.
        sql = """
                SELECT stream_id, count(*)
                FROM current_state_delta_stream
                WHERE stream_id > ?
                GROUP BY stream_id
                ORDER BY stream_id ASC
                LIMIT 100
            """
        txn.execute(sql, (prev_stream_id,))

        total = 0
        max_stream_id = prev_stream_id
        for max_stream_id, count in txn:
            total += count
            if total > 100:
                # We arbitarily limit to 100 entries to ensure we don't
                # select toooo many.
                break

        # Now actually get the deltas
        sql = """
                SELECT stream_id, room_id, type, state_key, event_id, prev_event_id
                FROM current_state_delta_stream
                WHERE ? < stream_id AND stream_id <= ?
                ORDER BY stream_id ASC
            """
        txn.execute(sql, (prev_stream_id, max_stream_id))
        return self.cursor_to_dict(txn)

    return self.runInteraction("get_current_state_deltas", get_current_state_deltas_txn)


# Traceback (synapse.api.errors.NotFoundError)
2019-04-26 06:18:26,267 - synapse.metrics.background_process_metrics - 203 - ERROR - presence.notify_new_event-13001- Background process 'presence.notify_new_event' threw an exception
Capture point (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/opt/synapse/synapse/synapse/app/homeserver.py", line 661, in <module>
main()
File "/opt/synapse/synapse/synapse/app/homeserver.py", line 657, in main
run(hs)
File "/opt/synapse/synapse/synapse/app/homeserver.py", line 648, in run
logger=logger,
File "/opt/synapse/synapse/synapse/app/_base.py", line 143, in start_reactor
run()
File "/opt/synapse/synapse/synapse/app/_base.py", line 127, in run
reactor.run()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/base.py", line 1267, in run
self.mainLoop()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/base.py", line 1276, in mainLoop
self.runUntilCurrent()
File "/opt/synapse/synapse/synapse/metrics/__init__.py", line 347, in f
ret = func(*args, **kwargs)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/base.py", line 875, in runUntilCurrent
f(*a, **kw)
File "/opt/synapse/synapse/synapse/storage/events_worker.py", line 349, in fire
d.callback([res[i] for i in ids if i in res])
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 460, in callback
self._startRunCallbacks(result)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1461, in _inlineCallbacks
status.deferred.callback(e.value)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 460, in callback
self._startRunCallbacks(result)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1461, in _inlineCallbacks
status.deferred.callback(e.value)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 460, in callback
self._startRunCallbacks(result)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1464, in _inlineCallbacks
status.deferred.errback()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 501, in errback
self._startRunCallbacks(fail)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1464, in _inlineCallbacks
status.deferred.errback()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 501, in errback
self._startRunCallbacks(fail)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1464, in _inlineCallbacks
status.deferred.errback()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 501, in errback
self._startRunCallbacks(fail)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1464, in _inlineCallbacks
status.deferred.errback()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 501, in errback
self._startRunCallbacks(fail)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1475, in gotResult
_inlineCallbacks(r, g, status)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
return g.throw(self.type, self.value, self.tb)
Traceback (most recent call last):
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/opt/synapse/synapse/synapse/storage/events_worker.py", line 419, in _enqueue_events
defer.returnValue({e.event.event_id: e for e in res if e})
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1362, in returnValue
raise _DefGen_Return(val)
twisted.internet.defer._DefGen_Return: {}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/opt/synapse/synapse/synapse/storage/events_worker.py", line 263, in _get_events
defer.returnValue(events)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1362, in returnValue
raise _DefGen_Return(val)
twisted.internet.defer._DefGen_Return: []

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/synapse/synapse/synapse/metrics/background_process_metrics.py", line 201, in run
yield func(*args, **kwargs)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/opt/synapse/synapse/synapse/handlers/presence.py", line 790, in _process_presence
yield self._unsafe_process()
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/opt/synapse/synapse/synapse/handlers/presence.py", line 805, in _unsafe_process
yield self._handle_state_delta(deltas)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/opt/synapse/synapse/synapse/handlers/presence.py", line 831, in _handle_state_delta
event = yield self.store.get_event(event_id)
File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/opt/synapse/synapse/synapse/storage/events_worker.py", line 120, in get_event
raise NotFoundError("Could not find event %s" % (event_id,))
synapse.api.errors.NotFoundError: 404: Could not find event None

# Fixed code
def get_current_state_deltas(self, prev_stream_id):
    """Fetch a list of room state changes since the given stream id

    Each entry in the result contains the following fields:
        - stream_id (int)
        - room_id (str)
        - type (str): event type
        - state_key (str):
        - event_id (str|None): new event_id for this state key. None if the
            state has been deleted.
        - prev_event_id (str|None): previous event_id for this state key. None
            if it's new state.

    Args:
        prev_stream_id (int): point to get changes since (exclusive)

    Returns:
        Deferred[list[dict]]: results
    """
    prev_stream_id = int(prev_stream_id)
    if not self._curr_state_delta_stream_cache.has_any_entity_changed(prev_stream_id):
        return []

    def get_current_state_deltas_txn(txn):
        # First we calculate the max stream id that will give us less than
        # N results.
        # We arbitarily limit to 100 stream_id entries to ensure we don't
        # select toooo many.
        sql = """
                SELECT stream_id, count(*)
                FROM current_state_delta_stream
                WHERE stream_id > ?
                GROUP BY stream_id
                ORDER BY stream_id ASC
                LIMIT 100
            """
        txn.execute(sql, (prev_stream_id,))

        total = 0
        max_stream_id = prev_stream_id
        for max_stream_id, count in txn:
            total += count
            if total > 100:
                # We arbitarily limit to 100 entries to ensure we don't
                # select toooo many.
                break

        # Now actually get the deltas
        sql = """
                SELECT stream_id, room_id, type, state_key, event_id, prev_event_id
                FROM current_state_delta_stream
                WHERE ? < stream_id AND stream_id <= ?
                ORDER BY stream_id ASC
            """
        txn.execute(sql, (prev_stream_id, max_stream_id))
        return self.cursor_to_dict(txn)

    return self.runInteraction("get_current_state_deltas", get_current_state_deltas_txn)