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

Changing state of started or joined application is deprecated....grrrr #1947

Closed
samuelcolvin opened this issue Jun 2, 2017 · 6 comments
Closed
Labels

Comments

@samuelcolvin
Copy link
Member

@samuelcolvin samuelcolvin commented Jun 2, 2017

Long story short

I very often want to set items on "frozen" app. I'm getting depreciation warnings for this and in future (presumably) this will fail outright. This is very frustrating.

This is made worse by pytest 3.1 which prints all warnings making this harder to ignore.

Expected behaviour

app['database'] = await create_my_database(...)

In a startup function should not cause deprecation warnings.

Actual behaviour

I'm getting warnings in lots of setups which I would have thought sensible.

Examples

in unit tests setting up a secondary server:

@pytest.fixture
def mock_external_server(loop, test_server):
    app = Application()
    app.router.add_post('/whatever.json', handler)
    server = loop.run_until_complete(test_server(app))
    app['server_name'] = f'http://localhost:{server.port}'
    return server

This causes a warning because I'm setting the server_name item on an app which is started.

using startup function:

async def startup(app: web.Application):
    app['pg_engine'] = await create_engine(pg_dsn(app['database']), loop=app.loop)

...

app = web.Application()
app.on_startup.append(startup)

(real life example here)

This again causes warnings setting pg_engine in a startup script.

Workaround

Surely this warning is wrong? If not what workaround should I use?

The only solution I can think of is an ugly bodge:

async def startup(app: web.Application):
    app['data']['pg_engine'] = await create_engine(pg_dsn(app['database']), loop=app.loop)

...

app = web.Application(middlewares=middleware)
app['data'] = {}
app.on_startup.append(startup)
@samuelcolvin samuelcolvin added the server label Jun 2, 2017
@asvetlov
Copy link
Member

@asvetlov asvetlov commented Jun 3, 2017

Hmm.
Initial intention for 'freezing' was prevention on route table modification after initialization phase.
After that I've though that freezing global state after on_startup is also good idea.

As I see in code for run_app this sequence is supported: .freeze() is called on first server socket creation, all on_startup signals are finished at this time.

@samuelcolvin
Copy link
Member Author

@samuelcolvin samuelcolvin commented Jun 3, 2017

import pytest
from aiohttp import web


async def startup(app):
    app['foobar'] = 123


async def handle(request):
    return web.Response(text='hello')


def create_app():
    app = web.Application()
    app.on_startup.append(startup)
    app.router.add_get('/', handle)
    return app


@pytest.fixture
def cli(loop, test_client):
    app = create_app()
    return loop.run_until_complete(test_client(app))


async def test_hello(cli):
    resp = await cli.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'o' in text

Then run

pytest --pythonwarnings=error test_example.py

gives: DeprecationWarning: Changing state of started or joined application is deprecated

@asvetlov
Copy link
Member

@asvetlov asvetlov commented Jun 14, 2017

The error was in aiohttp.test_utils. aiohttp.web.run_app was working as expected.
Fixed. Thanks for report.

@asvetlov
Copy link
Member

@asvetlov asvetlov commented Jun 14, 2017

Ooop, something is wrong with run_app too. I'll fix it.

@asvetlov asvetlov reopened this Jun 14, 2017
@asvetlov asvetlov closed this in 8f3d8f2 Jun 14, 2017
@asvetlov
Copy link
Member

@asvetlov asvetlov commented Jun 14, 2017

Finally fixed

@lock
Copy link

@lock lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.