# Buggy code
async def _unsafe_process(self) -> None:
    # If self.pos is None then means we haven't fetched it from DB
    if self.pos is None:
        self.pos = await self.store.get_user_directory_stream_pos()

    # Loop round handling deltas until we're up to date
    while True:
        with Measure(self.clock, "user_dir_delta"):
            room_max_stream_ordering = self.store.get_room_max_stream_ordering()
            if self.pos == room_max_stream_ordering:
                return

            logger.debug(
                "Processing user stats %s->%s", self.pos, room_max_stream_ordering
            )
            max_pos, deltas = await self.store.get_current_state_deltas(
                self.pos, room_max_stream_ordering
            )

            logger.debug("Handling %d state deltas", len(deltas))
            await self._handle_deltas(deltas)

            self.pos = max_pos

            # Expose current event processing position to prometheus
            synapse.metrics.event_processing_positions.labels("user_dir").set(max_pos)

            await self.store.update_user_directory_stream_pos(max_pos)


# Traceback (TypeError)
2021-02-16 17:36:09,420 - synapse.metrics.background_process_metrics - 211 - ERROR - user_directory.notify_new_event-9 - Background process 'user_directory.notify_new_event' threw an exception
Traceback (most recent call last):
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/metrics/background_process_metrics.py", line 208, in run
return await maybe_awaitable(func(*args, **kwargs))
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/handlers/user_directory.py", line 110, in process
await self._unsafe_process()
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/handlers/user_directory.py", line 159, in _unsafe_process
self.pos, room_max_stream_ordering
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/storage/databases/main/state_deltas.py", line 51, in get_current_state_deltas
prev_stream_id = int(prev_stream_id)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType

# Fixed code
async def _unsafe_process(self) -> None:
    # If self.pos is None then means we haven't fetched it from DB
    if self.pos is None:
        self.pos = await self.store.get_user_directory_stream_pos()

    # If still None then the initial background update hasn't happened yet.
    if self.pos is None:
        return None

    # Loop round handling deltas until we're up to date
    while True:
        with Measure(self.clock, "user_dir_delta"):
            room_max_stream_ordering = self.store.get_room_max_stream_ordering()
            if self.pos == room_max_stream_ordering:
                return

            logger.debug(
                "Processing user stats %s->%s", self.pos, room_max_stream_ordering
            )
            max_pos, deltas = await self.store.get_current_state_deltas(
                self.pos, room_max_stream_ordering
            )

            logger.debug("Handling %d state deltas", len(deltas))
            await self._handle_deltas(deltas)

            self.pos = max_pos

            # Expose current event processing position to prometheus
            synapse.metrics.event_processing_positions.labels("user_dir").set(max_pos)

            await self.store.update_user_directory_stream_pos(max_pos)