Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Summary

Fixes #4289 - When CrewAI is initialized from a non-main thread (e.g., in Streamlit, Flask, Django, Jupyter), the telemetry module was printing multiple noisy ValueError tracebacks for each signal handler registration attempt.

The fix adds a proactive main thread check in _register_shutdown_handlers() before attempting signal registration. If not in the main thread, a debug message is logged and signal handler registration is skipped entirely, avoiding the noisy exception tracebacks.

Changes:

  • Added thread check using threading.current_thread() is not threading.main_thread()
  • Log a debug message when skipping signal handler registration
  • Added 3 tests covering: signal handlers skipped in non-main thread, debug message logged, and signal handlers still registered in main thread

Review & Testing Checklist for Human

  • Verify the fix end-to-end: Run the reproduction script from the issue to confirm no more noisy tracebacks:
    import threading
    def run_crewai_in_thread():
        from crewai import Agent
        print("CrewAI imported successfully in thread")
    thread = threading.Thread(target=run_crewai_in_thread)
    thread.start()
    thread.join()
  • Confirm logger.debug() is the appropriate log level (issue suggested logger.warning() with docs link, but debug is less noisy which aligns with the goal)
  • Verify signal handlers still work correctly when running from main thread (normal usage)

Notes

When CrewAI is initialized from a non-main thread (e.g., in Streamlit,
Flask, Django, Jupyter), the telemetry module was printing multiple
ValueError tracebacks for each signal handler registration attempt.

This fix adds a proactive main thread check in _register_shutdown_handlers()
before attempting signal registration. If not in the main thread, a debug
message is logged and signal handler registration is skipped.

Fixes #4289

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

logger.debug(
"CrewAI telemetry: Skipping signal handler registration "
"(not running in main thread)."
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest enhancing the log message to be more actionable for users. The current message doesn't tell them how to disable the warning or where to learn more.

Suggested change:

logger.debug(
            "CrewAI telemetry: Skipping signal handler registration (not running in main thread). "
            "To disable this warning or disable crewai telemetry entirely, set CREWAI_DISABLE_TELEMETRY=true. "
            "See: https://docs.crewai.com/telemetry"
        )

@Bharath-designer
Copy link

Thanks, but I'd like to submit this fix myself as my first open source contribution.
I'll create a separate PR with an improved implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Noisy ValueError tracebacks when CrewAI initialized from non-main thread

1 participant