I’ve been entertaining an ongoing debate recently about whether distributed, async programming can be more easily safe and secure than traditional synchronous environments.
This has implications for blockchains like Ethereum, which are popularizing the idea of avoiding async operations, and also for secure distributed JavaScript in general, which through promises, eventual-sends, and promise pipelining makes it easier than ever to integrate complex asynchronous logic into some code.
I’d like to suggest that there may be a single pattern that is the most dangerous for async programming, and it the notion of un-locked memory access. You can see this in things like Mark Miller’s dislike of async/await
, and the general aversion to distributed programming.
I was trying to imagine how to make it as easy as possible to integrate these ideas at the language level, and even with promise pipelining it seems like a system would still be vulnerable to this kind of problem, so I don’t think it’s exclusively a problem with await
, it instead has to do with how hard it is to reason about exclusive rights to memory access.
For this reason, I think there may be room for a new operator, one equivalent to ~.
(eventual send), but one that represents getting an exclusive right to that object. The object would also need some mechanism for releasing this exclusive right, and so it would be like building mutexes into the language.
Let’s say we choose a new operator that is similar to ~.
, I’ll just use $.
for example here, I have no idea if this is taken or not. In this case, code like this would be safe:
const trainTicketRight = trainStation$.getTicket()
const hotelTicketRight = hotel$.getRoom()
if (trainTicketRight.price + hotelTicketRight.price < myBudget) {
trainTicketRight.purchase()
hotelTicketRight.purchase()
} else {
trainTicketRight.release()
hotelTicketRight.release()
}
I’m not certain there’s a benefit to this being at the language level: It’s possible having ~.getExclusive()
methods would be sufficient, since the host of the methods would be responsible for enforcing the exclusivity anyways, this may merely be syntactic sugar, but by integrating the notion of a mutex directly into the language, I was exploring whether it might be easier for a developer to reason about writing secure code, and so wanted to capture the idea somewhere, and this seemed like a good place.