Comments (7)
You are right, its my fault, but we need to stick with CatchableError
not Exception
.
from nim-libp2p.
Since the exception handling here is used for something similar to destructor clean-up, catching Exception
and re-raising seems appropriate.
from nim-libp2p.
well it's not - it's undefined behaviour when a Defect
is raised - by definition you're in an invalid state, so you can't unwind sanely.. you don't necessarily want to run destructors (ie perform a clean database shutdown like a destructor would) in that case because they'll be behaving in an undefined way.
from nim-libp2p.
The whole point of unwinding is to achieve graceful shutdown. Otherwise, it would be perfectly adequate to quit immediately. Raising a Defect
exception still comes with various guarantees about memory safety, so executing clean up code is quite reasonable. In general, such clean up code can take care of releasing distributed resources such as database transactions in flight, distributed locks and so on. Without doing this clean up, such resources will have to time out which is a less optimal solution.
Even locally, the OS takes care of releasing some resources automatically, but it may still leave files of the application lying around and other unwanted side effects that can be cleaned up.
from nim-libp2p.
but a Defect
by definition is a state that you did not anticipate - therefore you also cannot handle it gracefully, because the graceful shutdown code can no longer be expected to perform its task correctly - in fact, this is when it might cause further damage and simply letting go and not doing anything is as good as any other response.
the state of the objects you're trying to unwind is wrong and you don't know how. it's simply not consistent with the definition of Defect
to try to make sense of the situation - memory might be corrupt as well, in case of an indexoutofbounds for example.
what you're describing is appropriate behaviour for an exception - ie an error that is so unusual that it is an exception that it happens, but still within the realm of consistent and anticipated states, and graceful shutdown can happen: the transaction can be rolled back etc.
from nim-libp2p.
the root cause of this inconsistency is that the definition of Defect
is problematic: recoverable or not is generally contextual for the caller of the api, not its designer, and two Defect
:s are generally not the same: missing key during lookup in a map is a defect or not depending on how the map is used, not how it was designed - the map is not the right granularity to be making that call. same thing for arithmetic and overflows, same thing for array accesses etc etc
from nim-libp2p.
One of Nim's design goals is to ensure that the memory cannot be corrupted. The language tries to detect situations where this is about to happen and a Defect
exception is raised to abort the program in a controlled way.
You cannot anticipate all consequences of a Defect
, but it's quite possible to design stack objects with local invariants that provide enough guarantees to safety execute clean up code during unwinding.
Your problem with the map API is easy to resolve. A Defect
is something that violates the contract imposed by the creator of the map type. If the creator of the type requires that all key lookups are performed with existing keys, then the correct usage of the map is to first check for the key existence. Failing to do so is a bug in your usage code that must be fixed.
It's always possible to define such a contract for every API and a defect is just a violation of that contract. If you are not happy with the contract, you can create a different API offering a different contract.
from nim-libp2p.
Related Issues (20)
- Reevalute channel / stream / connection abstractions HOT 1
- Cannot run tests on Apple M1 MacOS HOT 2
- Bumper jobs failing HOT 11
- Graceful shutdown process reported as error in logs. HOT 4
- Where should people ask technical questions about nim-libp2p? HOT 1
- Where to have technical discussion about nim-libp2p? HOT 5
- fix(transport-interop): move code in libp2p/test-plans to this repo HOT 3
- minprotobuf does not handle `proto3` `repeated uint32` fields. HOT 6
- Single topic in RPC message
- WebTransport support
- Clear single-vote attestations when aggregate is full HOT 1
- GossipSub messages relayed back to source on 3 node network HOT 5
- peer doesn't respect backingOff
- switch.peerInfo reports with INADDR_ANY or INADDR6_ANY multiaddresses.
- rename nim-libp2p main branch from unstable to master HOT 1
- Prepare release 1.2.0
- IHave / IWant message behavior seriously impacts large message transmissions. HOT 7
- Support gossipsub 1.2.0 HOT 4
- Pubsubpeer has wrong protocol codecs
- flaky testdaemon
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim-libp2p.