London (CNN) The European Union has forced Britain to accept a six-month delay to the intractable Brexit process, with an option to leave earlier if the UK Parliament can agree a deal, at a tense summit of divided European leaders in Brussels.

After a working dinner that dragged long into the evening, leaders of the 27 remaining nations agreed another delay to Britain's scheduled withdrawal from the EU, this time until October 31. European leaders were unconvinced by Prime Minister Theresa May's promise that the UK would get its house in order to leave by the end of June.

But the deal was only reached after six hours of horse trading that saw European leaders divided over how to deal with Britain's ongoing political crisis. President of the European Council Donald Tusk, who had originally proposed an extension of up to a year, confirmed the UK had accepted the deal.

"This extension is as flexible as I expected and a little bit shorter than I expected, but it is still enough to find the best possible solution," he told reporters in Brussels. And he had a stark warning for British politicians who have been deadlocked for months: "Please do not waste this time."

May had sought a shorter delay to June 30 in order to avoid the UK crashing out without a deal later this week. The UK was meant to leave the EU on March 29, this was later extended to April 12.

The new extension, which falls on Halloween, means that Britain will more than likely have to hold elections for the European Parliament on May 23, nearly three years after the British people voted to leave the EU. May has always said she never wanted the UK to take part in those elections.

The fright night deadline, a day before the new European Commission is due to take office, was the product of grueling debate.

French President Emmanuel Macron, whose patience has worn thin and wants to get on with his own project of reforming the EU, had advocated for a shorter Brexit extension, while the German chancellor, Angela Merkel, argued the delay should be extended.

It is understood that Macron originally didn't see any reason to grant Britain a delay beyond the end of June, but was persuaded to accept the October deadline.

Macron did manage to force the other leaders to agree to a review process at the end of June, when Britain will be assessed on assurances that it will remain a constructive partner in EU business, even if it is on the way out of the bloc.

Speaking in the early hours of Friday, after the summit ended, Tusk hinted at the divisions, saying that despite their differing positions it was still "easier to find unity here than in the House of Commons."

"Until the end of this period, the UK will also have the possibility to revoke Article 50 and cancel Brexit altogether," Tusk said, noting that the delay allowed the UK to "reconsider"' its Brexit strategy.

European Commission President Jean-Claude Juncker said there would "probably" be European elections held in the UK, which "may seem a bit odd, but rules are rules."

If the UK does not take part in the European Parliament elections in late May it would be required to leave on June 1 without a deal, Irish Prime Minister Leo Varadkar said on Twitter.

The European leaders will reconvene in June to take stock.

London deadlock

While the European Union's decision to grant a so-called "flextension" allows the UK to leave as soon as it's ready, the optics aren't great for the Theresa May.

The prime minister suggested in a news conference following the summit that she could still manage to get her Withdrawal Agreement passed through Parliament in time to avoid the UK taking part in the European elections.

If the UK can agree a deal by May 22, the country will not have to hold European elections.

"I know that there is huge frustration from many people that I had to request this extension," May said, repeating that the UK must leave in a "smooth and orderly way."

British Prime Minister Theresa May speaks during a media conference at the end of an EU summit in Brussels.

The longer delay reflects doubts among EU leaders that May can succeed in swiftly winning cross-party support for her withdrawal agreement, which members of parliament have rejected three times.

It will also infuriate hardliners within her own Conservative Party, who view any further delay as a path to a softer Brexit, or a signal that leaving the EU may never materialize at all.

Hardline Brexiteers have indicated that, should the UK remain stuck in the union, then it will "become a Trojan horse within the EU," using British members of the European Parliament to create difficulties with key issues like the budget. That is a risk that the European leaders seem to have provided for.

Draft conclusions from the meeting, seen by CNN, instructed Britain not to interfere with the processes of the EU, with assurances aimed at satisfying Macron.

Juncker rebuked rumors that Britain would be a difficult partner should it find itself in the European Parliament, saying: "That's nothing new." He added that Britain's ability to block EU decisions was very limited.

The extra breathing space granted by the EU also comes with the express instruction that the Brexit deadlock in London needs to be broken.

It seems unlikely, given that cross party talks between the UK's Labour and Conservative parties have led nowhere. Sticking points seem to be the UK remaining in a customs union after Brexit, which Labour backs, but it's still divided on whether to ask British voters to ratify any deal in a second referendum.

The Irish backstop, a provision to prevent a hard border on the island of Ireland, also has to be resolved.

May said that further talks will take place with the opposition in the coming days.

"I do not pretend that the next few weeks will be easy or that there is a simple way to break the deadlock," she added.

But it remains to be seen whether May will last long enough to see her deal through in any shape or form.

There were already rumblings on Thursday night that the prolonged extension could spell an end to her leadership, triggering new elections.