BRUSSELS — About 48 hours before Britain was scheduled to crash out of the European Union, its Brexit deadline was postponed until Halloween.

The extension came after a high-stakes, six-hour summit in Brussels that ended early Thursday morning.

Twenty-seven prime ministers and presidents from across the continent had gathered to debate Britain's fate over a three-course dinner in Brussels' futuristic Europa Building.

British Prime Minister Theresa May was barred from attending what was effectively her country's sentencing.

The European leaders eventually emerged to offer the prime minister an Oct. 31 extension to sort out her Brexit mess. If before that date she achieves what she has failed to do so far, persuading British lawmakers to support her deal, then the extension would be terminated early.

It is the second time May has had to go to Europe pleading for more time. Brexit was initially scheduled to happen March 29, but at the last minute was pushed back to Friday.

The major upshot of this latest extension is that it saves the U.K. from leaving the E.U. in two days' time without any deal at all. Most experts warn a "no-deal Brexit" could tank the economy, bring food shortages, and risk conflict in Northern Ireland.

However, the agreement does little to solve the fundamental problems that have seen Brexit slide deep into chaos since June 2016, when Britain voted to leave the European Union.

"Please do not waste this time," European Council President Donald Tusk, who hosted the emergency summit, said at a news conference after the marathon dinner-meeting. "During this time, the course of action will be entirely in the U.K.'s hands."

By accepting the E.U.'s extension, May performed another reversal in a leadership that's become renowned for its high-profile 180-degree turns.

She previously had said she would not accept an extension beyond June 30 because it would clash with upcoming European elections. For a country trying to leave the E.U., participating in this vote would be a major logistical, diplomatic and political headache. This extension means that situation now looks likely.

Many in Brussels are deeply troubled by the idea of the U.K. remaining as a temporary yet powerful and potentially disruptive member. In a bid to address those fears, the E.U. extension calls on the U.K. "to act in a constructive and responsible manner" and give its "sincere cooperation" to the E.U. during the delay.

There will be a "review" at the European Council meeting in June to check this spirit is being upheld.

To avoid European elections and end the extension early, May must sell her deal to her fractious Conservative Party, with several of its members already in open rebellion because Brexit has been delayed. And ultimately she has to try to find cross-party support in the House of Commons, something that has spectacularly eluded her so far.

British lawmakers have voted down her deal three times, on two of those occasions handing it the heaviest and fourth-heaviest defeats in parliamentary history.

Even May's offer to resign in a bid to appease the holdouts did not get her the numbers.

"I do not pretend the next few weeks will be easy or that there is a simple way to break the deadlock in Parliament," she told a separate briefing at 3 a.m. Thursday. "But we have a duty as politicians to find a way to fulfill the democratic decision of the referendum, deliver Brexit and move our country forward."

She said that "nothing is more pressing or more vital."

The prime minister is set to make a statement to Parliament later in the day. Her team will continue discussions with the opposition Labour Party leader, Jeremy Corbyn, a rare and extremely belated act of bipartisan cooperation that has yet has failed to yield results.

With both their parties bitterly divided, these leaders could cause permanent splits and resignations no matter which course they take.

Indeed the struggle to find a solution has seen seismic ruptures in the fabric of British politics, with lawmakers emotionally and physically stretched, and each week bringing a new breakdown of parliamentary norms.

The chaos has seen growing calls for another public vote. Thousands of people recently marched through London calling for a second referendum. Senior government officials have said that the idea deserves consideration, something that seemed far-fetched even months ago.

Even more drastic, a petition to cancel Brexit altogether gained 6 million signatures on the Parliament's website.

Before a decision on the extension was announced, May was grilled by representatives of the 27 other E.U. members but was shut out of the room while they discussed her country's future over scallop salad, cod lion and iced macadamia nut parfait.

During this closed-door dinner, countries including Malta — population 500,000 — had more say over Britain's future than Britain itself.

Most countries were willing to grant a long, flexible extension to the U.K., but France was consistently a lone holdout, demanding Brexit be resolved sooner rather than later, according to four E.U. diplomats, speaking on condition of anonymity. But the leaders were required to reach their decision unanimously.

The eventual date appeared to be a compromise between those wanting a longer extension and France's shorter proposal. Tusk, who had favored a longer delay, said: "I think it's always best to have a piece of something than all of nothing."