Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logged exception "Failed to detach context" when testing Flask app #3919

Open
brianmcdonnell opened this issue May 17, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@brianmcdonnell
Copy link

brianmcdonnell commented May 17, 2024

Describe your environment
python 3.11
Flask==3.0.3
opentelemetry-distro==0.45b0

Steps to reproduce
import unittest
from flask import Flask
from app import app as flask_app # Replace 'your_flask_app' with the actual name of your flask app file

Implement the simple opentelemetry getting started Flask app:

from random import randint
from flask import Flask, request
import logging

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@app.route("/rolldice")
def roll_dice():
    player = request.args.get('player', default=None, type=str)
    result = str(roll())
    if player:
        logger.warning("%s is rolling the dice: %s", player, result)
    else:
        logger.warning("Anonymous player is rolling the dice: %s", result)
    return result


def roll():
    return randint(1, 6)

Write a test for it, and run the test.

class FlaskAppTestCase(unittest.TestCase):
    def setUp(self):
        flask_app.testing = True

    def test_rolldice(self):
        with flask_app.test_client() as c:
            response = c.get('/rolldice')
        self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
    unittest.main()

What is the expected behavior?
I expected the test to pass without any exception logging to the console. Even though it might not be typical to use opentelemetry when running unit tests, I'd still expect it to work without noisy error messages.

What is the actual behavior?
The test does pass, but it outputs noisy error messages.

ERROR:opentelemetry.context:Failed to detach context
Traceback (most recent call last):
  File "/Users/brianmcdonnell/src/otel-getting-started/venv/lib/python3.11/site-packages/opentelemetry/context/__init__.py", line 163, in detach
    _RUNTIME_CONTEXT.detach(token)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/brianmcdonnell/src/otel-getting-started/venv/lib/python3.11/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach
    self._current_context.reset(token)  # type: egnore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: <Token used var=<ContextVar name='current_context' default={} at 0x106b1b830> at 0x107b8e980> has already been used once

Additional context
This only happens when calling the test client from inside a with block. When the with block exits it tears down the context and logs this error. It appears that the opentelemetry Flask integration has already detached its context immediately after the request completed.

@brianmcdonnell brianmcdonnell added the bug Something isn't working label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant