Could massive derivatives exposures at banks lead to disaster? A while back I posted this question about the possible risks of banks having massive derivatives exposures.

The most upvoted answer (by BKay) said that there was little risk because banks mainly hold pairs of opposite bets. So for example it may simultaneously hold bets with two companies (A and B) where, if the price of oil goes up sharply then it has to pay out to A, but B has to pay the bank.

So this is all fine so long as both A and B are solvent after the oil price rise. Presumably if B goes bust then the bank is still liable to A.

So now my question is - couldn't there be some scenario where there is a sudden economic shock which simultaneously means that the banks will have to do a lot of paying out of bets but where the companies that are losing bets, and owe the banks money, go bust. With such enormous exposures it wouldn't take much of a fraction of the bet-losing companies going bust to cause an enormous loss for the banks.
Its possible, but, in principle, most non-financial companies use derivatives to "hedge risk", rather than to "bet". Hedging risk means that the firm will use derivatives to lower its chances of going bankrupt, for  example an oil company will buy a put option on its oil output, so that if the price of oil falls after the firm invested in producing it,  the firm can still sell the oil at a high price, and avoid bankruptcy. Other times, the bank is an intermediary in futures. futures allow a firm to sell its output forward in time, and another firm to buy the output forward in time. this reduces the risk for both of the firms. Of course, selling or buying  forward reduces the  chance of making  it big if the prices improve in your favor.

Also, financial regulation forces banks to consider multiple possible future scenarios and their effects on their balance sheet. These exercises result in a measure of the riskiness of the bank, and in turn let authorities ask the bank to hold more capital or unwind some of its positions if has too much risk of some sort. The exercises include dramatic swings in commodity prices, unemployment, or the failure of large corporations, or large financial counter-parties. 

There are certainly some financial agents out there that hold tons of risk. They might be indeed betting on one view or another, but because of the regulatory environment, these can't be "banks". This is very important, because if they are not banks, then their collapse does not then create contagion. A big investor looses his money, but if its not a deposit taking, (or similarly, a highly leveraged institution), then the losses, or gains, are in sense expected.

Therefore, although you are right in principle when you say that if banks stand in the middle between two firms and one of them goes bankrupt then the bank gets hit, regulation is meant to push banks away from risk. Also, the possible losses to the banks owners move them towards avoiding risk. And "risk" here includes the one that comes from their clients on one leg or another of a deal collapsing...

Here's a more detailed analysis: Congressional Research Service article on Financial Regulation.
You could also do worse than by taking Nobel Prize winner Bob Shiller's  online course.