question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Database connections being left open

See original GitHub issue

Python: 3.6.5 Django: 2.1.2 sentry-sdk: 0.4.2

I upgraded from raven to sentry-sdk yesterday and noticed my app was now keeping database connections open rather than closing them per request.

I wrote a tiny view to test this:

def sentry_test(request):
    from django.contrib.auth.models import User
    print(User.objects.all())
    return HttpResponse("<h1>Hello</h1>")

and when GET the URL repeatedly I see the subsequent connections remain idle after the request has completed:

=> SELECT state, query FROM pg_stat_activity WHERE client_addr IS NOT NULL;
 state  | query
--------+-----------------------------------------------------
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 active | SELECT state, query FROM pg_stat_activity WHERE client_addr IS NOT NULL;
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...

As you can see the connection from the migrate query on startup has remained open too.

If I use raven this behaviour is not exhibited.

I suspect this won’t help much, but if I use sentry-sdk and comment out:

        WSGIHandler.__call__ = sentry_patched_wsgi_handler

Then the problem doesn’t manifest itself but I’ve basically turned off sentry at this stage.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
untitakercommented, Oct 19, 2018

Thanks for the separate investigation 😃 will release asap

0reactions
jkeyescommented, Oct 19, 2018

Tested and verified connections are being closed correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common Reasons Why Connections Stay Open for a Long ...
Body. When you are monitoring connection pool performance, you may occasionally notice that connections are staying open for long periods of time.
Read more >
is it safe to keep database connections open for long time
Absolutely it is safe to do this. This is how client-server applications work. If you are using a three-tier application, the application ...
Read more >
Does leaving a connection open for a matter of seconds cause ...
I can only speak toward SQL Server. A database connection itself is not going to cause deadlocks. If it did connection pooling would...
Read more >
What If I leave Sql Connection Opened ? - MSDN
Connections hold open valuable resources to the database, consume memory, and can lock data that could cause other queries to slow down. So...
Read more >
Running Out of Database Connections - 3ap Engineering Blog
Your connection pool is running out of database connections. ... This points to the Transaction still being open during renderDocument .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found