diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index 7512446905..4777cafbe9 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -314,6 +314,9 @@ class DevicePlugin(Plugin): ''' Un-mount / eject the device from the OS. This does not check if there are pending GUI jobs that need to communicate with the device. + + NOTE: That this method may not be called on the same thread as the rest + of the device methods. ''' raise NotImplementedError() diff --git a/src/calibre/devices/mtp/windows/driver.py b/src/calibre/devices/mtp/windows/driver.py index 92e9790734..faa5296547 100644 --- a/src/calibre/devices/mtp/windows/driver.py +++ b/src/calibre/devices/mtp/windows/driver.py @@ -51,6 +51,7 @@ class MTP_DEVICE(MTPDeviceBase): self._main_id = self._carda_id = self._cardb_id = None self.start_thread = None self._filesystem_cache = None + self.eject_dev_on_next_scan = False def startup(self): self.start_thread = threading.current_thread() @@ -75,6 +76,10 @@ class MTP_DEVICE(MTPDeviceBase): @same_thread def detect_managed_devices(self, devices_on_system, force_refresh=False): if self.wpd is None: return None + if self.eject_dev_on_next_scan: + self.eject_dev_on_next_scan = False + if self.currently_connected_pnp_id is not None: + self.do_eject() devices_on_system = frozenset(devices_on_system) if (force_refresh or @@ -213,19 +218,24 @@ class MTP_DEVICE(MTPDeviceBase): return self._filesystem_cache @same_thread - def post_yank_cleanup(self): - self.currently_connected_pnp_id = self.current_friendly_name = None - self._main_id = self._carda_id = self._cardb_id = None - self.dev = self._filesystem_cache = None - - @same_thread - def eject(self): + def do_eject(self): if self.currently_connected_pnp_id is None: return self.ejected_devices.add(self.currently_connected_pnp_id) self.currently_connected_pnp_id = self.current_friendly_name = None self._main_id = self._carda_id = self._cardb_id = None self.dev = self._filesystem_cache = None + + @same_thread + def post_yank_cleanup(self): + self.currently_connected_pnp_id = self.current_friendly_name = None + self._main_id = self._carda_id = self._cardb_id = None + self.dev = self._filesystem_cache = None + + def eject(self): + if self.currently_connected_pnp_id is None: return + self.eject_dev_on_next_scan = True + @same_thread def open(self, connected_device, library_uuid): self.dev = self._filesystem_cache = None