justinpark opened a new pull request, #42767:
URL: https://github.com/apache/superset/pull/42767

   ### SUMMARY
   MCP tool-call/message audit logs (logs table) were recording user_id=NULL 
because LoggingMiddleware read g.user/get_user_id() either before auth ran or 
after the per-call Flask app context (pushed by _get_app_context_manager()) had 
already been popped. This adds a contextvars.ContextVar (_mcp_user_id_var, 
mirroring the existing _mcp_call_id_var pattern) that _setup_user_context() 
sets right after resolving the user, which the middleware reads in its finally 
block — surviving the app-context pop since it's tied to the asyncio task, not 
to Flask's g.
   
   ### TESTING INSTRUCTIONS
   
   - [x] Added unit tests for _setup_user_context() setting/clearing 
_mcp_user_id_var on success, failure, and guest users 
(test_auth_user_resolution.py)
   - [x] Added unit tests for LoggingMiddleware.on_call_tool/on_message 
preferring the ContextVar over the stale pre-call snapshot, with fallback when 
unset (test_middleware_logging.py)
   - [x] Verified new tests fail (ImportError) with the fix reverted, 
confirming they exercise the change
   - [x] Manually verified against a live dev instance: create_virtual_dataset 
and list_databases MCP calls now log the correct user_id in the logs table 
(previously NULL)
   
   ### ADDITIONAL INFORMATION
   <!--- Check any relevant boxes with "x" -->
   <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to