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

error is used in balanceR, maybe elsewhere #913

Open
simonmichael opened this issue Jan 27, 2023 · 9 comments
Open

error is used in balanceR, maybe elsewhere #913

simonmichael opened this issue Jan 27, 2023 · 9 comments
Labels

Comments

@simonmichael
Copy link

simonmichael commented Jan 27, 2023

Apparently balanceR functions in eg Data.Map.Internal and Data.Set.Internal can fail with a message like "Failure in Data.Map.balanceR" (related: #902). And this brought down (briefly) much of the Cardano blockchain: IntersectMBO/cardano-node#4826

Isn't it a bug to have such partial code in containers ?

@simonmichael simonmichael changed the title error used in Data.Map (Data.Set) error used in Data.Set, maybe elsewhere Jan 27, 2023
@simonmichael simonmichael changed the title error used in Data.Set, maybe elsewhere error is used in Data.Set, maybe elsewhere Jan 27, 2023
@treeowl
Copy link
Contributor

treeowl commented Jan 27, 2023

If our invariants are violated by someone external using internal functions incorrectly, then crashing with an error is the best outcome you can hope for. If you can reproduce the error without using any internal functions, then that's a bug in containers. Otherwise, you broke it and you get to keep both pieces.

@konsumlamm
Copy link
Contributor

Using error is no bug by itself. There obviously is a bug somewhere, but it's not clear wether it is in containers or in Cardano. Cardano uses Data.Set.Internal, which allows to break the internal invariants, which can trigger the error. See IntersectMBO/cardano-node#4826 (comment) for possible places where that might happen. Without a simple reproducible example, there is not much we can do here.

@simonmichael simonmichael changed the title error is used in Data.Set, maybe elsewhere error is used in Data.Map/Data.Set, maybe elsewhere Jan 27, 2023
@simonmichael simonmichael changed the title error is used in Data.Map/Data.Set, maybe elsewhere error is used in balanceR, maybe elsewhere Jan 27, 2023
@simonmichael
Copy link
Author

simonmichael commented Jan 27, 2023

Is this invariant sufficiently documented in the public API docs, eg https://hackage.haskell.org/package/containers-0.6.6/docs/Data-Map.html ?

How feasible would it be to provide a safer [default] API that can't be misused this way ?

I'm asking just because we all want to increase the ease of building reliable software with Haskell, containers is a very foundational library, and people often bemoan the inclusion of partial functions in base leading coders astray.

@treeowl
Copy link
Contributor

treeowl commented Jan 27, 2023

The internals are not in the public API. The invariant is documented in the source code. If you'd like to add or improve documentation, such a contribution will always be welcome. Similarly, if anyone has the time to add Liquid Haskell annotations proving that we maintain the invariants, those would be fantastic.

@simonmichael
Copy link
Author

simonmichael commented Jan 27, 2023

(I was assuming they are not calling containers' internal APIs, and indeed I find no reference to Data.Map.Internal or Data.Set.Internal in cardano-node).

@jwaldmann
Copy link
Contributor

that's the reference? https://github.com/input-output-hk/cardano-ledger/blob/master/libs/cardano-data/src/Data/MapExtras.hs#L32 ?

import Data.Map.Internal (Map (..), balanceL, balanceR, glue, link, link2)

@treeowl
Copy link
Contributor

treeowl commented Jan 27, 2023

@simonmichael That's because the internals are being used in some other Cardano package, if I understand the original ticket. Doesn't cardano-node import other Cardano things?

@treeowl
Copy link
Contributor

treeowl commented Jan 28, 2023

It wouldn't be bad to add Haddocks for balanceL and balanceR. They're documented in source, but it's not the most accessible documentation.

@meooow25
Copy link
Contributor

meooow25 commented Sep 7, 2024

My 2c is that we should not have Haddocks for such functions. It would encourage usage of the internal modules, which is not a good thing. Instead,

  • If someone want a missing functionality, they should create an issue to add it to the public module.
  • If the functionality is too niche to be added, they are forced to use the internal module. In that case it is best that they read the source code to understand what's going on, not just the Haddocks.

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

No branches or pull requests

6 participants
@simonmichael @jwaldmann @treeowl @meooow25 @konsumlamm and others