The time was shortly after 3am when David Cameron descended from level 80 of the vast Justus Lipsius building in Brussels on Friday. The birds were singing as he was whisked away for a much-curtailed sleep at the British ambassador’s residence, five minutes up the road.

The prime minister is no novice when it comes to long and tedious discussions at European summits. But what he had just witnessed over a seemingly never-ending dinner with the other 27 EU leaders was something different altogether.

The immediate crisis under discussion was migration and what the EU should do to handle the many thousands who have crossed the Mediterranean from Africa and the Middle East and arrived via Italy and the western Balkans over recent months. Increasingly, Europe is a magnet for those seeking a better life. But the EU does not know how to react and the problems are spreading. Last week a strike by French workers at Calais caused huge tailbacks on motorways leading to both the ferry port and Channel tunnel as hundreds of migrants – mainly from east Africa, the Middle East and Afghanistan – tried to take advantage of queueing traffic by breaking into lorries bound for the UK.

Against this background, a supposedly cordial working dinner, held high in the Council of Ministers building, rapidly descended into personal insults and finger-jabbing – which an exhausted-looking Cameron later summed up as “lengthy and, at times, heated discussions”.

Matteo Renzi, the Italian prime minister, was incensed by the refusal of several countries, including Hungary, which has taken in 60,000 refugees since the beginning of the year, and the Czech Republic, to agree to take part in a compulsory refugee-sharing scheme to help ease Italy’s burden. Cameron kept fairly quiet. The UK has opted out of EU asylum policy and Renzi, who was in an emotional state, did not need to be reminded of its non-participation. But others took up the cudgels as the row intensified across the table. Dalia Grybauskaite, the Lithuanian president, told Renzi in no uncertain terms that her country would not take part either. Bulgaria, one of the EU’s poorest countries, took a similar line. Disputes flared. European commission president Jean-Claude Juncker, prime mover behind the idea of compulsory burden sharing, and council president Donald Tusk tore strips off each other over what should be done, as inter-institutional solidarity broke down.

Facebook Twitter Pinterest Migrants attempting to board lorries at Calais. Photograph: Jeff J Mitchell

Angela Merkel said that the migration challenge was the most serious and difficult she had encountered in the EU during her time as German chancellor (Greece is also somewhat on her mind) as Renzi railed against fellow leaders for betraying the EU’s values. A voluntary scheme was all that was agreed. “Do as you like,” Renzi protested. “If this is your idea of Europe, keep it for yourself … you do not deserve to call yourself Europe. Either we have solidarity or we waste our time!”

Solidarity is indeed needed these days in a 28-nation EU that is creaking, not just under the weight of one crisis but several. Greece has been on the brink of defaulting on a payment to the IMF for weeks, placing its future in the euro and the EU in grave doubt, and is now heading for a defining referendum next weekend which could truly lead to Grexit. Meanwhile the UK is heading towards its own in/out referendum, also causing anxiety across European capitals. Nerves and tempers are fraying, as the migrants pour in. The EU is used to expanding its borders, and deepening ties, not losing members and limiting its role. Those from outside who regard their mission as to expose the EU as an unnatural political-cum-economic marriage of nations unsuited to union sense their moment.

In a dapper blue suit, Ukip leader Nigel Farage was prowling around the Brussels summit on Friday, declaring that the “triple crisis” was the beginning of the end for the EU, the moment it would be shown to have over-reached itself – when the poorer south would fracture from the wealthier north, when the consequences of open borders would divide members, and all would begin to implode. It was already happening with Greece. “I always said the euro would take 10 years to self-destruct and it is happening,” he said.

Greece’s problems haunted two days of talks. Meetings of heads of government and emergency gatherings of finance ministers and senior officials were called on and off in rapid succession, as a result of lack of progress but in full knowledge that a deal had to be done within days to avoid the first loss of a euro member and possible Grexit from the EU. All wrestled with how to reach a compromise with a leftwing government led by the combative Greek prime minister Alexis Tsipras. Greece has to transfer €1.54bn to the IMF by Tuesday, to unlock more bailout funds from Brussels. Without the money, Athens can’t afford to pay up and may be forced out of the single currency that was supposed to be the glue that bound Europe’s inner core of true believers together in perpetuity.

Facebook Twitter Pinterest Greek PM Alexis Tsipras – Greece may be forced out of the single currency if it doesn’t pay €1.54bn to the IMF by Tuesday. Photograph: Panayiotis Tzamaros/Corbis

Diplomats oscillated between rushes of optimism that Tsipras might give ground and accept more reform at home, including cuts to Greek pensions and a growing realisation that, in his Syriza movement, there are forces that might never bow to austere demands from distant Brussels authorities for fiscal discipline, whatever the price for Greece.

As one EU official put it on Friday: “We simply can’t second-guess them any more. We thought they would do a deal when it came to it … There are some who think it may be best to let them go.” Yesterday, ahead of the latest “emergency” meeting of finance ministers, there was more talk about a plan B for Greece (what to do to prevent a Greek default from causing economic contagion across the EU) than the endlessly discussed plan A (how to stitch a compromise together). Among diplomats there was a growing view that, if the wider damage to Europe could be contained, perhaps the EU was better off saying goodbye to Tsipras and Greece, than in spending endless time and money attempting to force a square peg into a round hole.

As for the “British problem”, it hardly raised its head during this summit. It was one crisis too many, and not the most pressing. That may not bode well for Cameron’s attempts to achieve a renegotiation of British membership before he holds an in/out referendum either next year or in 2017. The last thing EU leaders wanted was to spend precious time nit-picking over UK demands to amend treaties and change rules on founding principles, such as the freedom of movement of workers, which few will entertain anyway. Cameron had billed the meeting as the moment when he would launch his plans for reform and focus minds on the fact that the UK means business.

The original plan was that he would be given time at the start of the dinner meeting to lay out his stall in some style. As it was, they managed to squeeze him in for five minutes at 11pm – between their arguments over migration. Cameron said people in the UK were unhappy with the EU, but avoided much detail. One official said it was a welcome intermission which allowed people a little time to “redraft the conclusions on migration” – suggesting minds were not entirely on what the prime minister had to say.

Eight pages of summit conclusions on other matters ended with a fourth item of fewer than two lines, entitled just “UK”. It read: “The UK prime minister set out his plans for an in/out referendum in the UK. The European Council agreed to revert to the matter in December.” It is not that other EU leaders do not want to help Cameron, but more that they have not got much time or energy to do so. Talks will now grind on at official level for months over UK demands to curb welfare payments for EU migrants, demands to opt out of “ever-closer union”, demands to give more powers to national parliaments to block EU laws, and demands to ensure that what goes on inside the eurozone does not compromise the interests of those outside.

At this summit Cameron’s tone was different, less truculent. No doubt he realised that this was not the moment for UK grandstanding. He also seems to have accepted the need to build alliances, and that winning treaty changes in the EU for Britain requires a subtler, friendlier approach than when he came to Brussels to block things. This time he is the “demandeur” in the relationship, not the blocker. At his press conference after the summit, Cameron did something he has refused to do recently at home. He said clearly that he would prefer the UK to stay in the EU. “I believe it is beneficial for Britain,” he said, abandoning recent neutrality on the matter. A diplomatic memo leaked to the Guardian showed he had made a similar point to EU leaders.

Cameron opened his concluding press conference with a long, strong statement on the appalling terrorist attacks in Tunisia, France and Kuwait, stressing the need for global solidarity and cooperation to hunt terrorists and enhance security. Again, preaching about the national interest at such a moment would have seemed out of tune. With the EU so preoccupied, there were signs that Cameron may also now be soft-pedalling on his more controversial demands for treaty change, particularly the idea of banning EU migrant workers from claiming social benefits, such as tax credits or child benefit, for four years.

There was no mention of this specifically in his short talk at dinner, something that did not escape the notice of those who listened. It was whisked over in very general comments about reform as if it were just too incendiary to touch. The “British problem” as a whole was not of the moment. But, at some point soon, it will come to a head. If Cameron fails to achieve much more than the status quo – which may be the case – it will not escape the notice of Eurosceptics at home who will say it proves that the EU can never change and shows, therefore, that Britain should leave. EU leaders say they will do their best for Cameron, but not if he overplays his hand and refuses to speak up about the benefits of Europe.

As the EU’s crises multiply, and migration and terrorism threaten, there may be ill temper, tension and division at Europe’s core but there is an underlying determination to hold together. They talk differently now about losing the non-believers.

“We know the British debate,” said one EU diplomat discussing the referendum. “We really have to help where we can because the UK is important – but, in a way, it is like Greece: we cannot do so if the price is too high, and not if it means Europe cannot function as we want in the world.”