BRUSSELS — European Union leaders Thursday offered Britain an extension to Brexit that would allow the country to delay its EU departure date until Halloween.

Leaders of the 27 remaining EU member states met for more than six hours before agreeing after midnight to postpone Brexit until Oct. 31, two officials said.

The officials spoke to The Associated Press on condition of anonymity to discuss the closed-door negotiations. European Council President Donald Tusk confirmed in a tweet that an extension had been agreed to, but he did not disclose the date.

Tusk was meeting May, who had sought a delay only until June 30, to see if she would agree to the offer.

Just two days before Britain was due to leave the EU, its leaders spent a long dinner meeting wrangling over whether to save Britain from a precipitous and potentially calamitous Brexit, or to give the foot-dragging departing nation a shove over the edge.

May pleaded with them at an emergency summit to delay Britain’s exit, for a couple more months while the U.K. sorts out the mess that Brexit has become.

Some were sympathetic, but French President Emmanuel Macron struck a warning note.

“Nothing is decided,” Macron said as he arrived at the summit, insisting on “clarity” from May about what Britain wants.

“What’s indispensable is that nothing should compromise the European project in the months to come,” he said.

May believed that a June 30 deadline was enough time for Britain’s Parliament to ratify a Brexit deal and pass the legislation needed for a smooth Brexit.

But British lawmakers have rejected her divorce deal three times, and attempts to forge a compromise with her political opponents have yet to bear fruit.

May spoke to the 27 EU leaders for just over an hour, before they met for dinner without her to decide Britain’s fate. In contrast to some testy recent summits, there were signs of warmth and even humor. May and German Chancellor Angela Merkel were filmed laughing over a tablet bearing an image showing the two of them speaking to their respective Parliaments on Wednesday wearing similar blue jackets.

Many leaders had said they were inclined to grant a Brexit delay, though Macron had reservations after hearing May speak. An official in the French president’s office said the British leader hadn’t offered “sufficient guarantees” to justify a long extension.

Macron was concerned that letting Britain stay too long would distract the EU from other issues — notably next month’s European Parliament elections.

“Putting in danger the functioning of Europe is not preferable to a no-deal,” said the official, who was not authorized to be publicly named according to presidential policy.

Others had suggested a longer delay would likely be needed, given the depth of Britain’s political disarray.

May had signaled she would accept a longer extension, as long as it contained a get-out-early cause should Britain end its Brexit impasse.

“What is important is that any extension enables us to leave at the point at which we ratify the withdrawal agreement,” May said as she arrived in Brussels.

Before Wednesday announcement was made, May added that she was hopeful it could be as soon as May 22 — which would have avoided the need for Britain to participate in elections for the European Parliament.

If no extension had been granted Wednesday, Britain would have had to leave the bloc Friday with no deal, unless it canceled Brexit independently.

Economists and business leaders warn that a no-deal Brexit would lead to huge disruptions in trade and travel, with tariffs and customs checks causing gridlock and possible shortages of goods.