Skip to content

Commit 06b3a59

Browse files
committed
asyncio: error handling / documentation / fixes
1 parent 2d383a4 commit 06b3a59

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

mpd/asyncio.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def connect(self, host, port=6600, loop=None):
9898
self.__loop = loop
9999

100100
if '/' in host:
101-
r, w = await asyncio.open_unic_connection(path, loop=loop)
101+
r, w = await asyncio.open_unix_connection(path, loop=loop)
102102
else:
103103
r, w = await asyncio.open_connection(host, port, loop=loop)
104104
self.__rfile, self.__wfile = r, w
@@ -116,14 +116,27 @@ async def connect(self, host, port=6600, loop=None):
116116
self.__run_task = asyncio.Task(self.__run())
117117

118118
def disconnect(self):
119-
self.__run_task.cancel()
119+
if self.__run_task is not None: # is None eg. when connection fails in .connect()
120+
self.__run_task.cancel()
120121
self.__rfile = self.__wfile = None
121122
self.__run_task = self.__commandqueue = None
122123

123124
async def __run(self):
125+
# if this actually raises (showing as "Task exception was never
126+
# retrieved"), this is indicative of an implementation error in
127+
# mpd.asyncio; no network behavior should be able to trigger uncaught
128+
# exceptions here.
124129
while True:
125130
result = await self.__commandqueue.get()
126-
self._write_command(result._command, result._args)
131+
try:
132+
self._write_command(result._command, result._args)
133+
except Exception as e:
134+
result._feed_error(e)
135+
# prevent the destruction of the pending task in the shutdown
136+
# function -- it's just shutting down by itself
137+
self.__run_task = None
138+
self.disconnect()
139+
return
127140
while True:
128141
try:
129142
l = await self.__read_output_line()
@@ -252,6 +265,8 @@ def add_command(cls, name, callback):
252265
raise AttributeError("Refusing to override the %s command"%name)
253266
def f(self, *args):
254267
result = command_class(name, args, partial(callback, self))
268+
if self.__run_task is None:
269+
raise ConnectionError("Can not send command to disconnected client")
255270
self.__commandqueue.put_nowait(result)
256271
return result
257272
escaped_name = name.replace(" ", "_")

0 commit comments

Comments
 (0)