You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@michaelhays If you want to verify this would fix it, you can try calling close_old_connections yourself at the end of your consumer's method (but likely not in disconnect, as django/daphne#152 will prevent that). I'll look at solving both issues later in the week.
close_old_connections didn't work for me as I used it. I tried it in a couple of places.
End of the consumer method:
# consumers.pyclassChatbotConsumer(JsonWebsocketConsumer):
defconnect(self):
self.accept()
defreceive_json(self, content):
# create Chat in scope if one doesn't existif'chat'notinself.scope:
chat=Chat.objects.create_chat(self.scope['user'])
self.scope['chat'] =chat# process chatbot response, store messages, and send response to clientresponse_data=some_func(content['message'])
self.scope['chat'].add_message(content['message'])
self.scope['chat'].add_message(response_data)
self.send_json(response_data)
close_old_connections()
Setting CONN_MAX_AGE to 0 has helped a bit, in that certain actions like reloading the page removes 2 or 3 of the "COMMIT" idle connections (but, only sometimes?), and occasionally, the idle connections are reaped. But I've yet to notice any rhyme or reason to it.
OK, I have replicated this, and the temporary workaround is calling django.db.close_old_connections right after you do an ORM call, inside the same sync context (calling it anywhere else hides the connections due to thread safety).
OK, this should be fixed in 6afd423 - any code inside of a SyncConsumer will work fine, and anything you're calling from an async context should use the new channels.db.database_sync_to_async (can either be called with a function or be used as a function/method decorator). You'll also need the latest release of asgiref (2.1.4) to make method decorators work correctly.
If someone could confirm this fixes it for them, I can close this.
Hey, @andrewgodwin ! I'm steel having the same problem. And it's not only about consumer. I was following your tutorial (multichat). I have database_sync_to_async decorator that covers get_room_or_error in chat.utils. The main thing is that every page refresh (not only chat page) creates new database connection! Django + PostgreSQL + Daphne + Heroku
If you can reduce it to a simple set of code that reliably reproduces the problem, using the latest versions of all of the repositories, then please open a new issue with that code snippet, and steps to replicate locally (I can't debug things that only happen in Heroku)
It's exact same issue as @michaelhays described at django/daphne#163. In my case there is no such issue in local but it is in Heroku.
And I understand that you can't debug things that only happen in Heroku... But there's no support where I can text with that issue. You have more influence to report them about it as more people who updating from channels 1.0 to 2.0 will suffer this problem...
I just recreated everything on your clear multichat example here and deployed to heroku https://django-channels-2-test.herokuapp.com/ (user: demo, pass: demo) and get same issue. Refresh page 20 times and you'll get
OperationalError at /
FATAL: too many connections for role "jdknihvnjyczlm"
I was having this problem with channels and heroku postgres but it seems to have resolved itself when I changed my runtime from 3.6 to 3.8.1. I don't need to use close_old_connections()
You now what? i was struggling from last two days on this stupid error. I am glad you fixed it. Thanks a lot. I am really happy
I'm happy to see @Paulcappaert's suggestion to switch runtimes helped some here, unfortunately for some of us that's impossible as we may be using other libraries that don't support 3.8 yet. I for one rely on Celery for background processing and cannot really update.
For what it's worth, merely getting off of 3.6 may be enough - asyncio was a little underbaked in 3.6, and is much better in 3.7, and so most issues just need 3.7, not 3.8.
For what it's worth, merely getting off of 3.6 may be enough - asyncio was a little underbaked in 3.6, and is much better in 3.7, and so most issues just need 3.7, not 3.8.
OK, just a heads up that from my experience - that's not true. I was (and still am) using Channels 1, and when I upgraded to Python 3.7.2 to enjoy a Celery feature, I also had to upgrade to Channels 2 (all sorts of dependency clashes). That's when I encountered this issue again, and reverted my whole setup so Channels 2 didn't kill my server. I am hoping to give this upgrade another chance soon enough, after multiple failed attempts.
Channels 2 versus Channels 1 is a very different conversation given that they use completely different asynchronous styles under the hood.
Generally, this issue is caused by Django not identifying the connections with threads or coroutines that are already-dead. For Django itself this was fixed in the latest asgiref release, but Channels does not use the Local implementation in there so it doesn't get the same fixes. I also feel like Heroku does something to the Python environment based on a whole slew of Heroku-specific bugs that I used to get, but I have no time to verify that, unfortunately.
Channels 2 versus Channels 1 is a very different conversation given that they use completely different asynchronous styles under the hood.
Generally, this issue is caused by Django not identifying the connections with threads or coroutines that are already-dead. For Django itself this was fixed in the latest asgiref release, but Channels does not use the Local implementation in there so it doesn't get the same fixes. I also feel like Heroku does something to the Python environment based on a whole slew of Heroku-specific bugs that I used to get, but I have no time to verify that, unfortunately.
Let me clarify. Maybe I shouldn't have mentioned Channels 1 at all. What I was talking about is Channels 2 on Python 3.7.2, it's why I had to revert back to Channels 1, which is working perfectly fine.
I'm using Python 3.7.6 on heroku. My main server runs fine on with guinicorn. I observed this issue when I run daphne on my dev server.
On my dev server, I incorporated Channels 2 + Websocket and daphne into my app and saw this issue whenever I refreshed my page. I changed my app to use AJAX instead and still see this issue. I only have one view function that return JSONresponse for the AJAX call. That AJAX view function was decorated with 'login_required' and a database update. So i did the following:
Using AJAX + daphne:
comment out the database update in my Ajax function => same issue with multiple ajax calls.
comment out the login_require decorator on my ajax function=> no fatal on multiple ajax calls.
Using websocket(Channels2) + daphne
20+ Websocket calls with no database update in WebSocketConsumer=> no fatal
20+ Websocket calls with database update in WebSocketConsumer => fatal
Refresh the login_required view 20+ times: fatal.
I looked at the heroku logs and found that in all instances, the fatal occurred when Django was doing session validation. I don't observe this behavior on my main server when I refresh the login_required view 20+ times. My main server uses guinicorn.
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 58, in _get_user_session_key
return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 64, in __getitem__
return self._session[key]
I believe that Django session management is causing this issue when I use daphne. When either Django View or a WebSocket consummer requires validation, a connection is opened to the DATABASE but it never closes. This behavior is not apparent on the local machine because we don't have the limitation on database connection.
Activity
andrewgodwin commentedon Feb 6, 2018
@michaelhays If you want to verify this would fix it, you can try calling
close_old_connectionsyourself at the end of your consumer's method (but likely not in disconnect, as django/daphne#152 will prevent that). I'll look at solving both issues later in the week.michaelhays commentedon Feb 6, 2018
close_old_connectionsdidn't work for me as I used it. I tried it in a couple of places.End of the consumer method:
End of the model method:
Setting
CONN_MAX_AGEto 0 has helped a bit, in that certain actions like reloading the page removes 2 or 3 of the "COMMIT" idle connections (but, only sometimes?), and occasionally, the idle connections are reaped. But I've yet to notice any rhyme or reason to it.andrewgodwin commentedon Feb 7, 2018
Probably need the startup method connected as well. I'll look into it more tomorrow when I have time to work on Channels!
andrewgodwin commentedon Feb 7, 2018
OK, I have replicated this, and the temporary workaround is calling
django.db.close_old_connectionsright after you do an ORM call, inside the same sync context (calling it anywhere else hides the connections due to thread safety).Working on a proper fix now.
andrewgodwin commentedon Feb 8, 2018
OK, this should be fixed in 6afd423 - any code inside of a
SyncConsumerwill work fine, and anything you're calling from an async context should use the newchannels.db.database_sync_to_async(can either be called with a function or be used as a function/method decorator). You'll also need the latest release ofasgiref(2.1.4) to make method decorators work correctly.If someone could confirm this fixes it for them, I can close this.
michaelhays commentedon Feb 8, 2018
Confirmed to fix the issue for me!
andrewgodwin commentedon Feb 8, 2018
Great! I'll put this out in a release in the next day or two, depending on what else I land.
LeonidIvanov commentedon Mar 1, 2018
Hey, @andrewgodwin ! I'm steel having the same problem. And it's not only about consumer. I was following your tutorial (multichat). I have database_sync_to_async decorator that covers get_room_or_error in chat.utils. The main thing is that every page refresh (not only chat page) creates new database connection! Django + PostgreSQL + Daphne + Heroku
andrewgodwin commentedon Mar 1, 2018
If you can reduce it to a simple set of code that reliably reproduces the problem, using the latest versions of all of the repositories, then please open a new issue with that code snippet, and steps to replicate locally (I can't debug things that only happen in Heroku)
LeonidIvanov commentedon Mar 1, 2018
It's exact same issue as @michaelhays described at django/daphne#163. In my case there is no such issue in local but it is in Heroku.
And I understand that you can't debug things that only happen in Heroku... But there's no support where I can text with that issue. You have more influence to report them about it as more people who updating from channels 1.0 to 2.0 will suffer this problem...
LeonidIvanov commentedon Mar 1, 2018
I just recreated everything on your clear multichat example here and deployed to heroku https://django-channels-2-test.herokuapp.com/ (user: demo, pass: demo) and get same issue. Refresh page 20 times and you'll get
31 remaining items
sagardspeed2 commentedon Mar 31, 2020
ok thanks, i also updated runtime to python 3.8.2 and done
Hardeepsingh980 commentedon Apr 12, 2020
You now what? i was struggling from last two days on this stupid error. I am glad you fixed it. Thanks a lot. I am really happy
SHxKM commentedon Apr 12, 2020
I'm happy to see @Paulcappaert's suggestion to switch runtimes helped some here, unfortunately for some of us that's impossible as we may be using other libraries that don't support 3.8 yet. I for one rely on Celery for background processing and cannot really update.
andrewgodwin commentedon Apr 12, 2020
For what it's worth, merely getting off of 3.6 may be enough - asyncio was a little underbaked in 3.6, and is much better in 3.7, and so most issues just need 3.7, not 3.8.
SHxKM commentedon Apr 13, 2020
OK, just a heads up that from my experience - that's not true. I was (and still am) using Channels 1, and when I upgraded to Python 3.7.2 to enjoy a Celery feature, I also had to upgrade to Channels 2 (all sorts of dependency clashes). That's when I encountered this issue again, and reverted my whole setup so Channels 2 didn't kill my server. I am hoping to give this upgrade another chance soon enough, after multiple failed attempts.
I'm hosted at Heroku; YMMV.
andrewgodwin commentedon Apr 13, 2020
Channels 2 versus Channels 1 is a very different conversation given that they use completely different asynchronous styles under the hood.
Generally, this issue is caused by Django not identifying the connections with threads or coroutines that are already-dead. For Django itself this was fixed in the latest
asgirefrelease, but Channels does not use theLocalimplementation in there so it doesn't get the same fixes. I also feel like Heroku does something to the Python environment based on a whole slew of Heroku-specific bugs that I used to get, but I have no time to verify that, unfortunately.SHxKM commentedon Apr 13, 2020
Let me clarify. Maybe I shouldn't have mentioned Channels 1 at all. What I was talking about is Channels 2 on Python 3.7.2, it's why I had to revert back to Channels 1, which is working perfectly fine.
phamhm commentedon Apr 23, 2020
I'm using Python 3.7.6 on heroku. My main server runs fine on with guinicorn. I observed this issue when I run daphne on my dev server.
On my dev server, I incorporated Channels 2 + Websocket and daphne into my app and saw this issue whenever I refreshed my page. I changed my app to use AJAX instead and still see this issue. I only have one view function that return JSONresponse for the AJAX call. That AJAX view function was decorated with 'login_required' and a database update. So i did the following:
Using AJAX + daphne:
Using websocket(Channels2) + daphne
I looked at the heroku logs and found that in all instances, the fatal occurred when Django was doing session validation. I don't observe this behavior on my main server when I refresh the login_required view 20+ times. My main server uses guinicorn.
I believe that Django session management is causing this issue when I use daphne. When either Django View or a WebSocket consummer requires validation, a connection is opened to the DATABASE but it never closes. This behavior is not apparent on the local machine because we don't have the limitation on database connection.
carltongibson commentedon Apr 23, 2020
Hi. Are you able to reproduce on a laptop, or only on Heroku? (That still seems to be the issue...)
phamhm commentedon Apr 23, 2020
I only observe this behavior on heroku. I don't know how to limit database connection on my laptop.
Hardeepsingh980 commentedon May 2, 2020
Hey i fixed this by changing my heroku runtime to python 3.8. This fixed the issue. So, it is worth trying.
phamhm commentedon May 4, 2020
I switched to 3.8 and it has fixed my issue. That was very helpful, thank you.
dctalbot commentedon Jun 25, 2020
So glad I found this thread. Just chiming in here - the following manifested this behavior:
Dropping
conn_max_agefrom thedj_database_urlcall solves my immediate issue, but this just defaults it to 0 such that connections are never reused.I haven't tried upgrading Python yet, but I should do that and report back.
Bump runtime to latest python per this thread: django/channels#871
Reduce the risk of "FATAL: sorry, too many clients already"