@@ -59,6 +59,7 @@ def largest_file_index(self):
5959 return index
6060
6161 def stop_tasks (self ):
62+ self ._handle .save_resume_data ()
6263 while self .tasks :
6364 self .tasks .pop ().cancel ()
6465
@@ -152,13 +153,13 @@ def __init__(self, loop, executor):
152153
153154 async def add_fake_torrent (self ):
154155 tmpdir = mkdtemp ()
155- info , btih = _create_fake_torrent (tmpdir )
156+ info = _create_fake_torrent (tmpdir )
156157 flags = libtorrent .add_torrent_params_flags_t .flag_seed_mode
157158 handle = self ._session .add_torrent ({
158159 'ti' : info , 'save_path' : tmpdir , 'flags' : flags
159160 })
160- self ._handles [btih ] = TorrentHandle (self ._loop , self ._executor , handle )
161- return btih
161+ self ._handles [str ( info . info_hash ()) ] = TorrentHandle (self ._loop , self ._executor , handle )
162+ return str ( info . info_hash ())
162163
163164 async def bind (self , interface : str = '0.0.0.0' , port : int = 10889 ):
164165 settings = {
@@ -172,14 +173,12 @@ async def bind(self, interface: str = '0.0.0.0', port: int = 10889):
172173 self .tasks .append (self ._loop .create_task (self .process_alerts ()))
173174
174175 def stop (self ):
176+ while self ._handles :
177+ self ._handles .popitem ()[1 ].stop_tasks ()
175178 while self .tasks :
176179 self .tasks .pop ().cancel ()
177180 self ._session .save_state ()
178181 self ._session .pause ()
179- self ._session .stop_dht ()
180- self ._session .stop_lsd ()
181- self ._session .stop_natpmp ()
182- self ._session .stop_upnp ()
183182 self ._session = None
184183
185184 def _pop_alerts (self ):
@@ -271,9 +270,7 @@ def _create_fake_torrent(tmpdir):
271270 file_storage .add_file ('tmp' , size )
272271 t = libtorrent .create_torrent (file_storage , 0 , 4 * 1024 * 1024 )
273272 libtorrent .set_piece_hashes (t , tmpdir )
274- info = libtorrent .torrent_info (t .generate ())
275- btih = sha1 (info .metadata ()).hexdigest ()
276- return info , btih
273+ return libtorrent .torrent_info (t .generate ())
277274
278275
279276async def main ():
0 commit comments