It is no particular surprise that U.S. President Barack Obama is on the verge of turning over a new leaf with Iran. After all, over the course of his presidency, Obama has repeatedly emphasized that he would like the United States and Iran to overcome their 35 years of estrangement. What is surprising, however, is how rapprochement has come about -- not through negotiations over the fate of Tehran’s nuclear program, but as a result of the battle against ISIS.

Tehran and Washington find themselves on the same side in the fight against the Islamic State of Iraq and al-Sham (ISIS), also called the Islamic State (IS), and there are already signs that they have been cooperating against the extremist group’s advance through Iraq. Although there is no guarantee that this will last for the duration of the war, such cooperation is clearly a positive step.

The United States and Iran both view ISIS as a significant threat to their own interests. An ISIS stronghold near the Iranian border would be a profound and immediate security threat to Tehran. For one, the Sunni jihadists of ISIS are openly disdainful of the Shia faith, the sect of Islam that the overwhelming majority of Iranians and the majority of Iraqis adhere to. The group is already in a sectarian war in Syria and Iraq, and Tehran must assume that it eventually plans on turning its attention to Iran.

Washington, for its part, has also concluded that ISIS poses a significant threat. If ISIS manages to create a safe haven in Iraq, it could use the territory to plan operations against the West, undermine Western allies in the region, and endanger oil shipments in the Persian Gulf. In the meantime, the group’s war against the Iraqi state also poses a danger to U.S. interests. Over the past decade, Washington has paid a high price in blood and treasure to create a stable and relatively friendly Iraq. The collapse of that state would be a humiliating defeat.

Although the United States and Iran have different visions for the future of Iraq, they share three major strategic goals there: protecting Iraq’s territorial integrity; preventing a sectarian civil war that could easily metastasize into the entire region; and defeating ISIS. There is also a precedent of tactical cooperation in Iraq between Tehran and Washington: In 2001, the two cooperated to dislodge the Taliban from Afghanistan.

Obama has pledged not to tolerate the establishment of a terrorist state in Iraq and has already ordered limited air strikes against ISIS to protect U.S. personnel and facilities in Iraq and provide humanitarian relief to that country’s desperate Yezidi minority. Tehran has given unambiguous signals that it approves of Obama’s limited military mission. Iranian President Hassan Rouhani and a host of other officials have publicly expressed willingness to collaborate with the United States to defeat ISIS. It is unlikely that Tehran will offer tactical assistance to the United States on the battlefield, of course, but it is likely to welcome continued U.S. air strikes and might even quietly applaud the reintroduction of U.S. ground troops to Iraq.

But Obama has also declared, correctly, that there can’t be a U.S. military solution for Iraq’s problems until its political problems -- above all, its central government’s tendency toward exclusivism -- have been addressed. This is where Iran, which has maintained very close ties with the Shia parties that are dominant in Baghdad, has taken the lead. Washington has greeted the arrival of Iraq’s new prime minister, Haider al-Abadi. Abadi’s predecessor, Nouri al-Maliki, only stepped down after Iran (and Ayatollah Ali Sistani, Iraq’s leading Shia cleric) firmly pushed him to go. Tehran initially expressed its desire for Maliki to leave office in private. But when he still showed no signs of exiting, Ali Shamkhani, Secretary of Iran’s Supreme National Security Council, issued a public declaration congratulating Abadi for being named to form a new government. Tehran also mobilized Iraqi Shia groups as well as Shia militias to support Abadi. Washington was reduced to being an observer in much of this process, but it welcomed the outcome.

The cooperation between Tehran and Washington in Iraq has been productive so far, but it is also fragile. There are three factors that could easily derail it. The first is a dispute over the composition of the new Iraqi government. Iran recognized that Maliki had become too polarizing and authoritarian a figure, but that does not mean that it has otherwise revised its strategy that Iraq’s Shia community should dominate Iraqi politics, or changed its view that Sunni groups need to learn to accept Shia rule. As I wrote in an earlier article for Foreign Affairs, this is both a matter of principle (Shias comprise a comfortable majority of the Iraqi population) and pragmatism (Tehran believes that the Sunnis are less likely than the Shias and Kurds to be interested in building close ties with Iran).

Washington, by contrast, believes that Iraq’s Shia community should wield less power than it naturally would under strict proportionality according to population. In part, this may be because of pressure from Sunni governments in the region, including Saudi Arabia. But the United States also believes that some of Iraq’s Shia groups are more interested in acquiring a monopoly over national power than wielding power in a responsible fashion.

The second factor that could stall U.S.–Iranian cooperation is the prospect of an independent Kurdistan. Under Maliki, the relationship between Baghdad and the Kurdish regional capital of Erbil, became increasingly hostile. After the northern Iraqi city of Mosul fell to ISIS in June, the Kurds decided to seize the opportunity to make a bid for greater sovereignty. They quickly captured Kirkuk, a contested and energy-rich city in northern Iraq, and continued with their controversial policy to sell oil without Baghdad’s approval. They also stated their intention to hold a referendum on Kurdish independence.

All of these developments alarmed Tehran, which has generally maintained good relations with the Kurds, but has drawn a red line regarding Kurdish independence. The recent decision by Western countries to provide weapons directly to Kurdish militias has increased Tehran’s anxieties. Although Iran has developed close political and economic ties with Iraq’s Kurds and has even pledged to support them in their war against ISIS, Tehran also understands that independence for Iraqi Kurds could easily incite Iran’s own ethnic minorities to demand independence and undermine the country’s territorial integrity. Tehran is very aware of a recent precedent: After World War II, an independent government was fleetingly established in Mahabad, in Iranian Kurdistan, although the Soviet-backed movement was soon crushed by Iran’s central government. Iranian policymakers also know that, although the United States officially opposes Kurdish independence, the Kurds have powerful friends in Washington who seek to change that policy.

Finally, U.S.–Iranian cooperation can always falter because of the many constituencies in both countries that are ideologically opposed to any bilateral cooperation between the two states. In Washington, many blame Iran for encouraging sectarianism in Iraq, and correctly point out that Iran trained and funded the Shia militias that killed U.S. troops after the initial invasion of Iraq. They consider Iran to be the source of Iraq’s problems and sincerely, if unrealistically, seek to exclude it from any future security architecture of the country. For example, General James L. Jones, Obama’s former National Security Advisor, recently proposed convening a U.S.-sponsored strategic conference about Iraq. All regional players are to be invited to the conference, except Iran.

Similarly, many members of the security forces in Tehran reject cooperation with the United States. They believe that Washington is the source of instability in Iraq; some even blame the United States for the existence of ISIS, based on the conspiratorial belief that the United States helped finance the group so that it would fight against the Tehran-backed Assad regime in Syria. For Iran’s most devout Islamist ideologues, the United States can never be trusted beyond very short-term tactical cooperation.

Despite these difficulties, cooperation between Washington and Tehran is likely to deepen, rather than ebb, in the weeks ahead. ISIS is a clear transnational threat that demands a transnational solution. Iran has considerable experience fighting against ISIS in Syria and Lebanon and can offer much assistance to those who seek to eradicate the threat posed by the militant group. Indeed, the fight against ISIS may even produce the previously unthinkable: cooperation between Iran and Saudi Arabia, two countries that have more or less fought an open proxy war for the past several years in Iraq, Lebanon, and Syria. Now, both countries are threatened by ISIS, which explains why Saudi Arabia openly welcomed Abadi’s nomination to become prime minister.

Two weeks ago, Alaeddin Boroujerdi, chairman of the Iranian parliament’s Foreign Policy and National Security Committee, correctly stated that Iran, Saudi Arabia, and the United States are the key players in Iraq. If Washington and Tehran manage to cooperate to stabilize in Iraq, it would not only be good news for the Iraqis -- it could also pave the way for a final agreement in the ongoing nuclear negotiations. In that sense, the two countries would have truly achieved significant rapprochement, if not in the way that many observers originally anticipated.