Skip to content

Commit

Permalink
Bump pymysql version to 1.0.2 and simplify event timeout ping (CTFd#2392
Browse files Browse the repository at this point in the history
)

* Bump PyMySQL to 1.0.2
* Simply event timeout ping as discussed here: https://www.gevent.org/api/gevent.timeout.html
* Close db connections in the event handler route
  • Loading branch information
ColdHeat authored Aug 16, 2023
1 parent 4b393db commit ff80ef4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CTFd/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint, Response, current_app, stream_with_context

from CTFd.models import db
from CTFd.utils import get_app_config
from CTFd.utils.decorators import authed_only, ratelimit

Expand All @@ -19,4 +20,7 @@ def gen():
if enabled is False:
return ("", 204)

# Close the db session to avoid OperationalError with MySQL connection errors
db.session.close()

return Response(gen(), mimetype="text/event-stream")
20 changes: 8 additions & 12 deletions CTFd/utils/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ def subscribe(self, channel="ctf"):
# or else some reverse proxies will incorrectly buffer SSE
yield ServerSentEvent(data="ping", type="ping")
while True:
try:
with Timeout(5):
message = q[channel].get()
yield ServerSentEvent(**message)
except Timeout:
yield ServerSentEvent(data="ping", type="ping")
with Timeout(5, False):
message = q[channel].get()
yield ServerSentEvent(**message)
yield ServerSentEvent(data="ping", type="ping")
finally:
del self.clients[id(q)]
del q
Expand Down Expand Up @@ -109,12 +107,10 @@ def subscribe(self, channel="ctf"):
# or else some reverse proxies will incorrectly buffer SSE
yield ServerSentEvent(data="ping", type="ping")
while True:
try:
with Timeout(5):
message = q[channel].get()
yield ServerSentEvent(**message)
except Timeout:
yield ServerSentEvent(data="ping", type="ping")
with Timeout(5, False):
message = q[channel].get()
yield ServerSentEvent(**message)
yield ServerSentEvent(data="ping", type="ping")
finally:
del self.clients[id(q)]
del q
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SQLAlchemy-Utils==0.41.1
passlib==1.7.4
bcrypt==4.0.1
requests==2.28.1
PyMySQL[rsa]==0.9.3
PyMySQL[rsa]==1.0.2
gunicorn==20.1.0
dataset==1.5.2
cmarkgfm==2022.10.27
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pycparser==2.20
# via cffi
pydantic==1.6.2
# via -r requirements.in
pymysql[rsa]==0.9.3
pymysql[rsa]==1.0.2
# via -r requirements.in
pyrsistent==0.17.3
# via jsonschema
Expand Down

0 comments on commit ff80ef4

Please sign in to comment.