Hacker News new | past | comments | ask | show | jobs | submit login

> how would they would have made the error easier to spot?

There was a try block with two function calls, and it seems like the author assumed only the first function call could throw.

With return-based errors, the error handling wouldn't implicitly be shared between the two functions. The author would write the handling for the first function, and then they would notice the second function could return an error state too. Then they might write a correct handler, or at least we should expect them to say "this can't happen, so panic". This bug would have been trivial to fix if it panicked instead of corrupting fds.




What makes you think that "the author assumed only the first function call could throw?"

Programmers in exception languages never think (or at least should never think) "X cannot throw" because it is never true. Any function has the potential of throwing an exception. This is even more true in Python than in other exception throwing languages.

No, the problem here is that the author has realized that they need special cleanup logic in the case that a file fails to be opened, but they applied that logic too broadly.


So for reference the second line is:

  return _TemporaryFileWrapper(file, name, delete)
> What makes you think that "the author assumed only the first function call could throw?"

Because it's almost true, and the code cleanup is clearly designed around just the first function.

_TemporaryFileWrapper does nothing in its __init__ except copy the four parameters into the object. There's no reason to anticipate a problem.

But thinking about it more precisely, there's an even worse issue.

Even if neither function has an error, if you get a KeyboardInterrupt between the object creation and the actual return, everything gets corrupted.

People aware of the bug, trying to fix this code, suggested a nested try/catch, and I think that would still explode if the timing was exactly wrong.

Because really, who thinks about a return statement throwing? It's an unintuitive problem that can only happen with this style of exceptions.

It's probably even worse than that. If an exception can hit right after the file object is made then you get a similar problem...

Is there anything in Python that restricts the timing of KeyboardException or is this entire adventure on extremely treacherous territory?


That's precisely the point I was making: there is no python code which is guaranteed not to throw. If the original author thought that the line of code in question wouldn't throw, they were were wrong and fundamentally misunderstood Python.


Well the code wouldn't have caused any errors if it only caught normal exceptions, at least.

> That's precisely the point I was making: there is no python code which is guaranteed not to throw.

Well the first thing you said is "what is your alternative to exceptions, and how would they would have made the error easier to spot? Its not obvious that the error would have been easier to spot if it had been written using Go's `!= nil` pattern or Rust's `?` pattern."

In those languages, you can't have an error state trigger between lines of code, or inside a line of code that does basic things like assigning to a local variable or returning.

It won't happen in C++ either (with certain assumptions).

Python is a language that's supposed to be straightforward, and very few coders are going to understand exactly how hostile exceptions can be. So while these assumptions shouldn't have been made, the blame goes toward the design of the language. An alternative system would be better.


> Well the code wouldn't have caused any errors if it only caught normal exceptions, at least.

Uhh... it was a normal exception that triggered the bug in question.

> Well the first thing you said is

Yes, that's the first thing I said. But the point I'm making here is that your initial response to that is probably wrong: "the author assumed only the first function call could throw" That's just not how you think in Python. (Or at least it is not how you should think.)

My point is this: the bug wasn't assuming that the second function wouldn't fail. The bug was assuming that the same cleanup code was appropriate in the case of either failure. That same bug could easily exist with return codes as it does in exceptions.

> Python is a language that's supposed to be straightforward

In fairness to Python, this is an atypical case. The problem arises because this is low level code dealing directly with fds. Most of the time, code will deal with higher level objects which will properly clean up after themselves regardless of what crazy stuff you do with them.


> Uhh... it was a normal exception that triggered the bug in question.

That doesn't count because it was an invasive monkeypatch that took a function that could not possibly cause an error and made it throw.

> My point is this: the bug wasn't assuming that the second function wouldn't fail. The bug was assuming that the same cleanup code was appropriate in the case of either failure.

Maybe. Hard to know for sure.

> In fairness to Python, this is an atypical case.

I dunno, pretty much any code that uses a catch block to undo things has to be very carefully written. Even if you're wrapping things in higher level objects, you still have ugly scenarios where you're mutating a data structure and have to unwind the changes halfway through. I bet tons of that code is written under the assumption that there will be no exceptions.

I guess if we consider "trying to catch KeyboardInterrupt at any level without immediate exit" as a weird barely-supported case, then things are fine.


> That doesn't count because it was an invasive monkeypatch that took a function that could not possibly cause an error and made it throw.

The original claim was based on that possibility, its pretty goofy to declare it doesn't count.

> I dunno, pretty much any code that uses a catch block to undo things has to be very carefully written. Even if you're wrapping things in higher level objects, you still have ugly scenarios where you're mutating a data structure and have to unwind the changes halfway through. I bet tons of that code is written under the assumption that there will be no exceptions.

Is it any different for return codes than for exceptions? I'd say that any code which attempts to undo change in the case of an error has to be carefully written. Every special case on the ordinary code path, has to have matching special cases on the error paths. In fact, I've never seen code in any language that does such a thing. And I wouldn't trust if it I did, since its extremely difficult to get right for all cases, and the errors paths almost certainly hasn't been well tested.

> I guess if we consider "trying to catch KeyboardInterrupt at any level without immediate exit" as a weird barely-supported case, then things are fine.

Well, trying to continue running after KeyboardInterrupt is dubious, the user just asked the program to terminate.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: