From a4de7678481ec1edbec498e069932645ecb85751 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 29 Jun 2019 10:17:50 +0530 Subject: [PATCH] Linux: Workaround for bug in recent Linux kernels that causes the Kindle to eject after calibre connects to it. Fixes #1834641 [Opening Kindle devices in Calibre will cause a disconnect from Linux LTS 4.19.51+ onwards](https://bugs.launchpad.net/calibre/+bug/1834641) --- src/calibre/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 92240d1f77..007a92777e 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -660,3 +660,16 @@ def ipython(user_ns=None): def fsync(fileobj): fileobj.flush() os.fsync(fileobj.fileno()) + if islinux and getattr(fileobj, 'name', None): + # On Linux kernels after 5.1.9 and 4.19.50 using fsync without any + # following activity causes Kindles to eject. Instead of fixing this in + # the obvious way, which is to have the kernel send some harmless + # filesystem activity after the FSYNC, the kernel developers seem to + # think the correct solution is to disable FSYNC using a mount flag + # which users will have to turn on manually. So instead we create some + # harmless filesystem activity, and who cares about performance. + # See https://bugs.launchpad.net/calibre/+bug/1834641 + # and https://bugzilla.kernel.org/show_bug.cgi?id=203973 + with open(fileobj.name + '.linux-sucks', 'wb') as f: + f.write(b'I cannot believe I need to do this') + os.remove(f.name)