BRUSSELS — European Union leaders on 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.

Belgium_EU_Brexit_68409

British Prime Minister Theresa May arrives for an EU summit at the Europa building in Brussels on Wednesday. Associated Press/Francisco Seco

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, due on Friday, 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 believes that a June 30 deadline is 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 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 is concerned that letting Britain stay too long would distract the EU from other issues – notably next month’s European Parliament elections.

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

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

May 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.

She added that she was hopeful it could be as soon as May 22 – a key date since that would avoid the need for Britain to participate in elections for the European Parliament.

Several months have passed since May and the EU struck a deal laying out the terms of Britain’s departure and the outline of future relations. All that was needed was ratification by the British and European Parliaments.

But U.K. lawmakers rejected it – three times. As Britain’s departure date of March 29 approached with no resolution in sight, the EU gave Britain until Friday to approve a withdrawal plan, change course and seek a further delay to Brexit, or crash out of the EU with no deal to cushion the shock.

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 at British ports and possible shortages of goods.

A disorderly Brexit would hurt EU nations, as well as Britain, and all want to avoid it.