Repository navigation
Conversation
|
@lehins can you also add a regression test to the new testsuite in the |
|
@hvr Done. Added |
|
lgtm, I don't think this modification is likely to be relevant in the wild, but who knows. I looked around for some similar functions, e.g. IMO, another acceptable implementation would be to |
I'd actually prefer this, because requesting something with negative size (I'm on the fence though whether to consider size == 0 an error) smells like a bug to me somehwere in the code which we shouldn't silently cover up by clamping to 0... |
|
@hvr and @mitchellwrosen I do agree with both of you that a negative value for the size is incorrect, it was the source of the bug afterall, together with incorrect conditional checks in
Moreover As far as size |
It is true that it's not clear at a type level that the negative value is invalid, but I feel
I think it's a common pattern to both adjust the value to make it valid and blow up with an
This is an interesting suggestion, but I'm not convinced that being able to catch a "negative size provided to newTBQueue" is very useful. The fix is the same in either case: provide a positive number instead ;) I would also like to clarify that I don't speak with any authority over this codebase or anything, and this patch is totally fine with me, for what it's worth. I just find the discussion interesting so I figured I'd respond point by point :) |
|
I find this topic extremely interesting as well! Hence are the long comments ;)
Very much so. Even Just to be clear, I didn't recommend
It's really not, but if you do want to make your code safe, then you are forced to catch all exceptions, which isn't nice. Moreover, doing a All of the above are just my thoughts on the topic. And since I really don't like |
|
Well, this comes down to the fundamental design choice in APIs of whether to validate input parameters and, in case of invalid or nonsensical parameters, fail loudly, or whether to succeed silently (there'd actually be a 3rd option if we allowed for logging side-effects: succeed loudly by emitting warnings). Both approaches have their merit but different trade-offs and a similar discussion came up when deciding whether As we know, the partial subtraction was picked, and there's a function now GHC.Natural.minusNaturalMaybe :: Natural -> Natural -> Maybe Naturalby which you can easily implement saturated subtraction if you combine that with e.g. newTBQueue :: Num n => STM (Maybe (TBQueue a))and map invalid input parameters to a NB: We're about to release PS: If we had a loudly failing q <- newTBQueue (n `max` 0)or maybe you'd even want q <- newTBQueue (n `max` 1)or even clamp both sides q <- newTBQueue ((n `max` 1) `min` 1024)The other way round is more awkward IMO. |
|
@hvr I think switching to Would you like me to go ahead and switch both of these? The actual internal implementation of size and capacities should stay as PS. I meant all three, of course, I wouldn't skip |
…drop support for GHC <7.10
I disagree that it isn't a problem. In fact, you reminded me about checking the rest of Note that Moreover, when incrementing |
|
@hvr Very good point. I'll go ahead and switch to Natural everywhere in the TBQueue codebase.
|
…f tarvis for GHC < 7.10
|
@hvr Another approach would be to clamp TBQueue bound to Also did drop support for GHC < 7.10 due to Looked in other places in the library where integral numbers are used and only thing I found that could be fixed was in |
* Switch to Natural in signalTSem * make newTSem accept Integer * Got rid of deriving Typeable * Removed redundant CPP
dffeba0 to
a6b6363
Compare
* Switch to Natural in signalTSem * make newTSem accept Integer
|
@lehins Thanks! I've manually merged your commits to the master branch (while slightly rearranging them); if you have time, please review them in case I missed something. |
|
@hvr Looks great. Thanks. Totally forgot about |
This PR fixes inconsistent behavior described below by enforcing TBQueue size to be always non-negative and fixing the capacity checks in the
writeTBQueuefunction.Supplying negative size to both
newTBQueueIOandnewTBQueueyields aTBQueuethat is still writable. We end up with queue that is both full and empty, yet we can still write into it as much as we like (theoreticallymaxBound :: Wordin fact, due to wraparound):