Provider Users Watcher Audit¶
Overview¶
This document outlines potential issues found in the Provider Users Watcher code that could cause crashes in staging. The watcher is a critical component that handles OAuth token exchange, service activation, and email sync initialization.
Critical Components¶
1. OAuth Token Exchange¶
credentials = await self.event_loop.run_in_executor(None, fetch_token)
refresh_token = credentials.refresh_token
access_token = credentials.token
granted_scopes = credentials.scopes
2. Service State Management¶
self.previous_services[provider_user_id] = current_services.copy()
3. Document Change Handling¶
def on_provider_users_update(
self,
doc_snapshot: list[DocumentSnapshot],
changes: list[DocumentChange],
read_time: str,
) -> None:
4. Background Task Management¶
asyncio.create_task(run_email_sync())
5. Error Handling and Recovery¶
except Exception as e:
self.logger.report(
f"Error in handle_modified for provider user {provider_user_id}",
e,
extra={"provider_user_id": provider_user_id},
)
6. Repository Operations¶
provider_user = await self.provider_user_repo.get_by_id(doc.id)
if not provider_user:
raise ValueError(f"Provider user {doc.id} not found")
7. Event Loop Management¶
future = asyncio.run_coroutine_threadsafe(
self.handle_change(change), self.event_loop
)
8. Memory Management¶
self._futures: list[asyncio.Future] = []
9. Initialization and Shutdown¶
def unsubscribe() -> None:
listener.unsubscribe()
for future in watcher._futures:
if not future.done():
future.cancel()
10. Logging and Monitoring¶
self.logger.report(
"Error in on_provider_users_update", e, extra={"error": str(e)}
)
Next Steps¶
Priority should be given to: 1. Implementing proper error recovery mechanisms 2. Adding timeouts and retries for external operations 3. Improving task management and resource cleanup 4. Adding proper transaction handling 5. Implementing comprehensive monitoring 6. Adding proper shutdown handling 7. Improving memory management 8. Adding proper validation at all levels 9. Implementing proper state management 10. Adding proper logging and metrics