Giter Site home page Giter Site logo

Comments (4)

rolfmorel avatar rolfmorel commented on August 29, 2024

All right, I think I've got it.

In InputDevice.close() it first checks if self.fd looks like a valid file descriptor, then it calls os.close(self.fd).

The problem is if os.close(self.fd) raises an exception self.fd will still be the value which caused the exception even though the OS considers the file descriptor now closed. This in turn means that when the destructor (del) is called it will call self.close(), and the close method will consider self.fd to look valid, and because the fd is still the value which the OS has already closed os.close(self.fd) it will now generate a 'Bad file descriptor' error.

The quick hack solution:

def close(self):
    if self.fd > -1:
        fd = self.fd
        self.fd = -1

        os.close(fd)

Or the much better pythonic version (any Exception handlers keep easy access to the offending file descriptor):

def close(self):
    if self.fd > -1:
        try:
            os.close(self.fd)
        finally:
            self.fd = -1

Note that the above also fixes the 'self.fd > 0' check.

from python-evdev.

gvalkov avatar gvalkov commented on August 29, 2024

Hello. I'm very happy to hear that this project is of use to you!

I openly admit that I didn't know constructors in Python worked this way. Thanks for catching this bug and enlightening me in the process. Does the fix in 12c5751 work for you? I'll push 0.4.3 as soon as we settle this.

The fd > 0 when stdin was de-allocated thing is also a good catch. Much appreciated.

Regards,
Georgi

from python-evdev.

rolfmorel avatar rolfmorel commented on August 29, 2024

Thank you for looking it over and quickly applying a fix.

Just gave 12c5751 a quick test and it looks like the erroneous behaviour is fixed and everything is working as expected.

from python-evdev.

gvalkov avatar gvalkov commented on August 29, 2024

evdev-0.4.3 pushed to pypi. Thanks again.

from python-evdev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.