There has been very little to celebrate in Brazil of late. The Latin American giant has become the global epicentre of the coronavirus pandemic, suffering record death tolls last month and more new infections than any other country.

Yet last week, just before the anniversary of the 1964 military coup that ushered in a long dictatorship, the defence minister and the three armed forces commanders unexpectedly gave cause for cheer. They suddenly parted ways with far-right president Jair Bolsonaro in a textbook display of loyalty to the democratic constitution.

Bolsonaro has already triggered a severe health crisis by spending the past year playing down the importance of the pandemic, resisting mask-wearing, mocking vaccination and refusing to implement lockdowns.

To this unenviable record, the mercurial former army captain has added a crisis with Brazil’s powerful military. Supposedly his allies, the armed forces were wooed by Bolsonaro with hundreds of government posts — including the vice-presidency and almost half of the cabinet — as well as generous increases in military spending.

Yet when the president called on “my armed forces” to help him resist attempts by governors and mayors to impose lockdowns, the generals balked. Given a choice between showing fealty to an erratic and unpredictable president who has openly scorned Congress and the courts or pledging allegiance to Brazil’s constitution, they wisely chose the latter.

The departing defence minister, Fernando Azevedo e Silva, was a Bolsonaro appointee and the two men were close. Yet in his farewell letter, the general made a point of saying that he had “preserved the armed forces as institutions of the state”.

The armed forces chiefs’ commitment to institutionality follows the supreme court’s commendable firmness in resisting Bolsonaro’s attempts to take emergency powers or to veto lockdowns imposed by local authorities. Congress, too, has asserted itself with a “yellow warning” to Bolsonaro’s government to change course or face possible impeachment.

Much could still go wrong. Bolsonaro has named replacement military chiefs whom he hopes will prove more pliant. An open fan of Brazil’s 1964-85 dictatorship, he appeared last year at demonstrations calling for the closure of Congress and the supreme court. This sparked fears he might be flirting with the idea of suspending democracy and ruling by decree with the backing of the armed forces, as Alberto Fujimori did in Peru in his “autogolpe” of 1992.

Brazil holds a presidential election next year and Bolsonaro’s failures on the pandemic are hurting the economy and his own popularity. Ex-president Luiz Inácio Lula da Silva, still an icon for Brazil’s left despite a chequered record in government, is likely to run against him after the supreme court annulled his corruption convictions.

The prospect of a highly polarised vote between the far-right and the traditional left raises another concern: if defeated, Bolsonaro might attempt a Trumpian claim of a stolen election and rally his supporters, including rank-and-file troops and police, for a Capitol Hill-style assault on Brasília.

Such fears may be overdone. Yet Bolsonaro has repeatedly shown scant regard for democracy or for the lives of his fellow citizens. As his popularity declines and his re-election prospects fade, the risk of him gambling on an open challenge to democracy rises.

In such a febrile atmosphere, the firm commitment of the military commanders, the Congress and the judiciary to uphold the world’s fourth biggest democracy is a vital and positive sign.