Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle IOError exceptions #4

Closed
wants to merge 2 commits into from
Closed

Handle IOError exceptions #4

wants to merge 2 commits into from

Conversation

guidoiaquinti
Copy link

@guidoiaquinti guidoiaquinti commented Jun 16, 2016

If the entire service or the control/status files do not exist or we are not able to read/write the files, raise an exception instead of failing with IOError

I don't know if this is a good solution as it's not backward compatible with the implementation that are already catching the exception. Suggestions are welcome

f.write(signal)
except IOError as e:
raise ServiceControlError("Unable to write service control file '{0}'".format(e.filename))
else:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of sketchy because of the write fails, we still want to close the file.

Probably initialize f = None outside of the try block and do a conditional close in a finally:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something like this?

f = None
try:
    f = open(self._control,"w+")
    f.write(signal)
except IOError as e:
    if f is not None:
      try:
        f.close()
       except IOError:
        pass
    raise ServiceControlError("Unable to write service control file '{0}'".format(e.filename))

What about splitting the 2 operations in different try/exc blocks?

try:
    f = open(self._control,"w+")
except IOError as e:
    raise ServiceControlError("Unable to open service control file '{0}'".format(e.filename))
try:
    f.write(signal)
except IOError as e:
    raise ServiceControlError("Unable to write service control file '{0}'".format(e.filename))
finally:
    f.close()   

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking:

f = None
try:
    f = open(...)
    f.write(...)
except IOError as e:
    raise SuperviseControlError(...)
finally:
    if f is not None:
        f.close()

Splitting and the extra try around the close is probably overkill, but I'm not used to weird scenarios that you can't close fds on.

@fmoo
Copy link
Owner

fmoo commented Jun 17, 2016

Since this is a breaking change, please update VERSION to 2.0.0

@guidoiaquinti
Copy link
Author

While doing some GitHub cleanup 🧹 I found this old PR from 2016.

After 6 years waiting, I think I'm good to close this. Thank you anyway @fmoo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants