During a March 2014 visit to Saudi Arabia, among the officials who met President Barack Obama was then-Crown Prince Salman bin Abdulaziz al-Saud. Salman became king after the death of his half-brother, King Abdullah, on Jan. 23, 2015. (Saul Loeb/AFP/Getty Images)

In the late 1980s, a U.S. diplomat in Riyadh went to ask a small favor from then-Crown Prince Abdullah bin Abdul Aziz, who replied: A friend who doesn’t help you is no better than an enemy who does you no harm.

Now the United States and Saudi Arabia need each other’s help as much as ever, as the Middle East shudders from instability that stretches from Syria to Iraq to Yemen, spawning terrorist threats as well as threats to the legacy of American intervention in Iraq and the Saudi leadership role in the Arab world.

President Obama, who was to arrive in India on Sunday morning to attend Republic Day celebrations, will drop plans to visit the Taj Mahal and make a detour Tuesday to Riyadh. There he will pay his respects to the late King Abdullah, who died Thursday, and firm up ties to the new king, Salman bin Abdul Aziz, who inherits this slate of problems along with his crown.

“As a leader, he was always candid and had the courage of his convictions,” Obama said of Abdullah in a statement Friday. “One of those convictions was his steadfast and passionate belief in the importance of the U.S.-Saudi relationship as a force for stability and security in the Middle East and beyond.”

For much of the Obama presidency, however, Saudi Arabia has wondered whether the United States had fallen into the category of an unhelpful friend, and it has doubted U.S. commitment to the region.

President Obama will visit Saudi Arabia Tuesday and meet its new King Salman, ending his planned three-day trip to India early. (Reuters)

Diplomats say that King Abdullah had been angry in recent years at Obama’s failure to topple Syrian leader Bashar al-Assad and that he was disappointed with the lack of U.S. pressure for an Israeli-Palestinian settlement and anxious about whether U.S.-led nuclear talks with Iran would lead to a rapprochement between the United States and Saudi Arabia’s main rival.

But U.S. officials say that over the past few months, ties between Washington and Riyadh have warmed again, bolstered by an Obama visit to the Saudi capital last March and, more importantly, by both countries’ overriding focus on blunting the rise of the Islamic State, also known as ISIL or ISIS.

“The relationship is on a sound track,” said a senior administration official. “I don’t want to paint a picture of complete harmony. They want us to be more aggressive on Iran. They want us to be more aggressive in Syria,” he said. But, he added, “I think on both sides there has been an evolution. I think the threat of ISIL has certainly brought the two countries together.”

He said that the Saudis see the Islamic State as a direct threat to their internal stability and that during a December visit by Prince Mohammed bin Nayef, the Saudi internal security chief, “there was no obvious difference of views” and “on issue after issue we have been able to agree on a way forward.”

The stakes in the relationship are high. The United States needs help from Saudi Arabia, home of the extremist Wahhabi strain of Islam, in tracking down terrorists and choking off funding to militant jihadist groups. And as the world’s largest crude oil exporter, Saudi Arabia’s stability is critical for the world economy. Saudi Arabia, for its part, needs U.S. help to protect its vast oil infrastructure and shipping lanes for its oil tankers.

At the same time, Saudi Arabia is fending off threats now lapping at its borders, especially the rise of Shiite groups backed by Iran and the erosion of Saudi prestige as a leader of Sunnis throughout the Middle East, Saudis and Western observers say.

“The situation is urgent,” said Abdullah al-Shammari, a political analyst in Riyadh. “Saudi Arabia needs to think about new alternatives and creative solutions. Otherwise, we will lose even more.”

Recently, Saudi fighter jets have taken part in bombing raids against Islamic State forces, rare forays for a country that has long preferred to work behind the scenes. And the Saudi government has hunted down returning Islamic State warriors while preaching a more moderate brand of Islam to head off recruiting efforts by extremist groups.

“I think the Saudis have started to see ISIL and the jihadi blowback as increasingly threatening to them,” said F. Gregory Gause, head of the international affairs department at the Bush School of Government and Public Service at Texas A&M University. “That has mitigated their enthusiasm for the Syrian revolt. Now much more attention is being paid to inoculating the home front and criminalizing those aiding the jihad.”

The emergence of a new common foe, however, might not be the best way to rebuild a relationship, especially since the foe has emerged from Iraq. Abdullah had opposed the U.S. invasion of Iraq, but when President George W. Bush asked him to let U.S. forces use northern Saudi Arabia as a staging area, Abdullah granted the favor.

Since then, say some former American diplomats, the relationship with the kingdom has changed, from one in which favors, mutual understanding and personal ties play important roles to one comprising a litany of case-by-case requests. In the space of a month, the Obama administration can host a leading Saudi prince, condemn a public lashing and then praise Saudi cooperation on fighting the Islamic State.

“Now everything is transactional,” said Chas W. Freeman Jr., a veteran diplomat who was U.S. ambassador to Saudi Arabia under President George H.W. Bush. “The Saudis now ask: What’s in it for us? Everything is one-off. There is a fundamental shift in the relationship.”

That relationship dates to Feb. 14, 1945, when President Franklin D. Roosevelt met for five hours with King Abdul Aziz ibn Saud on the deck of a U.S. destroyer in Egypt’s Great Bitter Lake to plot out a postwar order for the kingdom, where an American company had discovered oil in 1938.

By the 1970s, President Richard M. Nixon saw Saudi Arabia and Iran, then ruled by the shah, as “twin pillars” of regional stability and bulwarks against Soviet influence in the region during the Cold War. After the Soviet invasion of Afghanistan and the fall of the shah, President Jimmy Carter vowed to do whatever was necessary to protect Saudi Arabia and the flow of oil through the Strait of Hormuz.

The participation of Saudi citizens in the terrorist attacks on Sept. 11, 2001, and the flow of Saudi charity money to groups such as al-Qaeda turned many Americans against the kingdom, but relations between the two governments remained strong, if often troubled.

For most of the Obama presidency, U.S.-Saudi relations have been marked by tension, having mostly to do with Obama’s handling of the Syrian civil war.

Abdullah was angry that Obama didn’t do more to overthrow Assad, or punish the Syrian regime for using chemical weapons in the conflict, or give guns to the Syrian rebel groups. Obama had called the use of chemical weapons a “red line.”

“The irony is that the genuine, legitimate and moderate opposition — which Saudi Arabia would like to see supplied with defensive weapons . . . which the West continues to deny them — is now fighting both Assad’s forces and their allies from Shia forces on one side and the so-called Sunni jihadists al-Qaeda fighters and other bloodthirsty irregulars on the other,” Prince Turki al-Faisal said in a London speech in May last year. “This is a shameful situation and a dark blot on the moral standing of the world.”

In an earlier speech, Turki had said that “public opinion in the kingdom, and I think in all of the Muslim world, is very much disappointed in the way that the United States has dealt with this issue, along with the Palestinian issue.”

But the Obama administration doubted that arming the rebels would succeed, and it feared the extremism of some of the rebel groups. Many of the weapons others have supplied have found their way into the hands of the extremist Islamic State, which now controls territory along parts of Saudi Arabia’s northern border.

Meanwhile, the more moderate groups in which Saudi Arabia invested most of its energies have suffered a string of defeats, and the Syrian Opposition Coalition has proved unable to present a meaningful political alternative to Assad. Earlier this month, Saudi-backed candidates lost to ones backed by Turkey in elections for positions in the opposition’s leadership, further shrinking Saudi influence in Syria.

Abdullah also broke openly with the United States over Egypt, deploring the lack of U.S. support for the Mubarak regime. Later, when the Obama administration cut aid to Egypt, Saudi Arabia pledged to provide the lion’s share of a $12 billion Persian Gulf aid package to help new Egyptian leader Gen. Abdel Fatah al-Sissi, who had ousted an elected Muslim Brotherhood government.

Obama’s March 28 visit last year came amid these tensions. “It was less intended to accomplish something than to preclude bad things from happening,” said Freeman, “and I think it worked on that level. There had been a whole series of Saudi statements criticizing the United States. That is not the Saudi style that we know. And it reflected genuine exasperation.”

Saudi Arabia has also feared that a deal on Iran’s nuclear program could lead to a warming of U.S. relations with its rival Iran and that prolonged negotiations might sap U.S. desire to confront Tehran as it expands its influence in Beirut, Baghdad, Damascus and Sanaa.

“They do raise the fact that we need to be more aggressive about countering Iran’s threat in the region, and they wonder whether nuclear talks constrain our ability to counter Iran’s efforts in region,” the senior U.S. official said. “Our answer is no. We can compartmentalize talks. And a nuclear-armed Iran is more dangerous to Saudi Arabia than any agreement we might reach.”

However, the collapse of the government in Yemen last week at the hands of groups backed by Iran will only heighten Saudi concerns about the stability of its southern border. The Iranian-backed Shiite Houthi movement now exercises de facto control over Saudi Arabia’s southern neighbor, compounding a sense of encirclement that began with the empowerment of Iraq’s Shiite majority after the U.S. invasion in 2003.

The Saudis “see another Hezbollah being created, but at their border,” said Mustafa Alani, director of security and terrorism at the Geneva-based Gulf Research Center, referring to the Iran-backed movement in Lebanon. “They look to the northern border and see that Iraq is 100 percent in the hands of Iran, and they look to the southern border and see what may be an Iranian mini-state in Yemen.”

In addition to the strategic threat posed by a hostile power on its doorstep, the Houthi ascent in Yemen has inflicted a psychological blow to Saudi Arabia’s sense of prestige as the region’s leading Sunni power and anointed guardian of Islam, said Shammari, the political analyst.

“Forget about the strategic importance — this is a Saudi, a Sunni and an Arab issue,” he said. “Saudi Arabia is the sponsor of the Sunni world, and when Arabs see Houthis controlling Yemen, this destroys respect for Saudi Arabia.” He added, “King Salman has to see this and act.”

Opposed to Bush’s decision to invade Iraq, Abdullah was also worried about Obama’s decision to withdraw, which reinforced fears that the United States, flush with new oil supplies and talking about a foreign policy and military “pivot” to Asia, would abandon the Persian Gulf.

“I think we always tend to be a bit in crisis mode about the Saudis,” Gause said. “This whole idea about America leaving the Middle East is an exaggeration but is very intensely felt in the gulf states because they rely on us for military protection.”

Some regional experts have harsher assessments. “Saudi foreign policy is reactive, it’s erratic and it’s personality-driven, depending on the princes who hold portfolios,” said Frederic Wehrey of the Carnegie Endowment for International Peace. “It’s all about bags of cash handed out to unreliable allies.”

Where will U.S.-Saudi relations go after Abdullah? Though Abdullah introduced some progressive reforms in Saudi Arabia, the unequal treatment of women and suppression of dissent creates a certain distance from the United States. Human Rights Watch noted in August, the same month that American James Foley was beheaded by Islamic State militants, that 19 people were beheaded in Saudi Arabia, eight of them for nonviolent offenses such as drug smuggling.

Salman, who had served as defense minister since 2011, has pledged continuity. Reputed to be more conservative on religious matters, he isn’t likely to introduce sweeping changes.

But the cultural gap is narrowing. Many members of the next generation of Saudi leaders studied in the United States, where there are currently about 54,000 Saudis studying. Gause said that the new generation “learned at their fathers’ knees that America was their big relationship.”

Gause added that “the old Cold War solidarity” has disappeared, but he said that “interests will keep the two states together.”

Sly reported from Beirut. Hugh Naylor in Beirut contributed to this report.