Skip to content

Commit f6e1df0

Browse files
author
Jack Kordas
committed
Stop using abstract socket because of uvloop
uvloop is used by UvicornWorker for perfomance MagicStack/uvloop#528
1 parent 5e5dc19 commit f6e1df0

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

client/globus_cw_client/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def _checktype(value, types, message):
1313
raise TypeError(message)
1414

1515

16-
def log_event(message, retries=10, wait=0.1):
16+
def log_event(message: str | bytes, retries=10, wait=0.1):
1717
"""
1818
Log the @message string to cloudwatch logs, using the current time.
19-
message: bytes (valid utf8 required) or unicode.
19+
message: bytes (valid utf8 required) or str.
2020
retries: number of retries to make on failed socket connection
2121
wait: number of seconds to wait between retries
2222
Raises: exception if the message is too long or invalid utf8
@@ -27,7 +27,7 @@ def log_event(message, retries=10, wait=0.1):
2727
# python3 json library can't handle bytes, so preemptively decode utf-8
2828
if isinstance(message, bytes):
2929
message = message.decode("utf-8")
30-
_checktype(message, str, "message type must be bytes or unicode")
30+
_checktype(message, str, "message type must be bytes or str")
3131

3232
_checktype(retries, int, "retries must be an int")
3333
if retries < 0:
@@ -37,9 +37,7 @@ def log_event(message, retries=10, wait=0.1):
3737
if wait < 0:
3838
raise ValueError("wait must be non-negative")
3939

40-
req = {}
41-
req["message"] = message
42-
req["timestamp"] = int(time.time() * 1000)
40+
req = {"message": message, "timestamp": int(time.time() * 1000)}
4341
return _request(req, retries, wait)
4442

4543

@@ -62,17 +60,19 @@ async def log_event_async(message, retries=10, wait=0.1):
6260
return await _request_async(req, retries, wait)
6361

6462

63+
socket_path = "/tmp/org.globus.cwlogs"
64+
65+
6566
def _connect(retries, wait):
6667
"""
6768
Try to connect to the daemon @retries + 1 times,
6869
waiting @wait seconds between tries
6970
Raise: Exception if max attempts exceeded
7071
"""
71-
addr = "\0org.globus.cwlogs"
7272
for _ in range(retries + 1):
7373
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
7474
try:
75-
sock.connect(addr)
75+
sock.connect(socket_path)
7676
except Exception as err:
7777
sock.close()
7878
error = err
@@ -84,10 +84,10 @@ def _connect(retries, wait):
8484

8585

8686
async def _connect_async(retries, wait):
87-
addr = "\0org.globus.cwlogs"
8887
for _ in range(retries + 1):
88+
writer = None
8989
try:
90-
reader, writer = await asyncio.open_unix_connection(path=addr)
90+
reader, writer = await asyncio.open_unix_connection(path=socket_path)
9191
except Exception as err:
9292
if writer:
9393
writer.close()

client/test/test_log_bad_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@pytest.mark.parametrize("message", (object(), {"foo": "bar"}))
66
def test_log_event_rejects_bad_message_type(message):
7-
with pytest.raises(TypeError, match="must be bytes or unicode"):
7+
with pytest.raises(TypeError, match="must be bytes or str"):
88
log_event(message)
99

1010

daemon/globus_cw_daemon/daemon.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,16 @@ def main():
208208
_print("cwlogs: starting...")
209209
_log.info("starting")
210210

211+
addr = "/tmp/org.globus.cwlogs"
212+
213+
# clean up previous socket if exists
214+
try:
215+
os.remove(addr)
216+
except FileNotFoundError:
217+
pass
218+
219+
os.umask(0)
211220
listen_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
212-
addr = "\0org.globus.cwlogs"
213221
try:
214222
listen_sock.bind(addr)
215223
except OSError as e:

0 commit comments

Comments
 (0)