After An Lushan launched his rebellion, he quickly captured the eastern capital Luoyang and sent a large army toward Chang'an.
Emperor Xuanzong of Tang, seeing the dire situation, immediately sent the great general Geshu Han to defend Tong Pass. Tong Pass was the last line of defense for Chang'an; if it fell, Chang'an would be in grave danger. Geshu Han fully understood the importance of Tong Pass, so he dared not be negligent and did everything possible to fortify it. Over the next six months, the rebel forces launched multiple attacks on Tong Pass, but none succeeded in capturing it. After analyzing the situation, Geshu Han concluded that the rebels, having committed atrocities—burning, killing, and looting along their march—had lost the people's support. If they continued to fail in taking Tong Pass, their morale would inevitably waver. At that point, the Tang army could seize the opportunity to strike a decisive blow, and the rebels would surely collapse.
The situation at that time was indeed as Geshu Han had analyzed, becoming increasingly favorable for the Tang army. Many regions organized resistance against the rebel forces and achieved one victory after another. After Emperor Xuanzong of Tang received reports of victories from various places, he became full of confidence in suppressing An Lushan's rebellion. Prime Minister Yang Guozhong, blinded by temporary successes, continuously slandered others and urged Emperor Xuanzong to order Geshu Han to lead his troops out of Tong Pass to engage the rebels. Emperor Xuanzong, losing his reason, repeatedly pressed Geshu Han to fight. Geshu Han kept writing to the emperor, warning that the rebels were full of cunning schemes and that it was unwise to engage them lightly. However, Emperor Xuanzong completely ignored these warnings and continued to urge Geshu Han to march out and confront the rebels. Knowing the consequences of defying imperial orders, Geshu Han reluctantly complied and led his forces out to face the enemy.
Geshu Han led the Tang army to camp at Xiyuan in Lingbao (present-day Lingbao, Henan). The rebel forces deliberately feigned weakness, luring the Tang army into an ambush, which inflicted heavy casualties—out of 200,000 Tang soldiers, only 8,000 managed to escape back to Tong Pass. Geshu Han regrouped his troops and planned to hold Tong Pass at all costs. However, the rebels soon attacked Tong Pass. Seeking to save themselves, Geshu Han's subordinates seized him and surrendered to the rebels, taking him along. As a result, Tong Pass fell into enemy hands.
After the news of Tong Pass falling reached Chang'an, Emperor Xuanzong of Tang was greatly shocked. He knew clearly that the rebel forces would soon attack Chang'an, and the city's troops were insufficient to resist them. If troops were summoned from other regions, they could not arrive in time. The emperor was extremely anxious and summoned his ministers early the next morning to discuss countermeasures, but none of them could propose a solution. Finally, Prime Minister Yang Guozhong suggested taking refuge in Chengdu. Emperor Xuanzong hesitated for a long time without making a decision. Yang Guozhong then sent Yang Guifei's sisters, the Ladies of Qin, Han, and Guo, into the palace to persuade Emperor Xuanzong and Yang Guifei to take refuge in Chengdu.
Why did Yang Guozhong insist on persuading Emperor Xuanzong of Tang to go to Chengdu? It turned out that besides being the Prime Minister, Yang Guozhong was also the military governor of Jiannan (present-day Chengdu, Sichuan). Right after An Lushan launched his rebellion, he had already sent people to store large quantities of supplies in Chengdu. Moreover, his enmity with An Lushan ran deep as the ocean—if the rebel forces captured Chang'an, he would surely be torn to pieces by An Lushan. Therefore, he tried every possible means to convince Emperor Xuanzong to go to Chengdu.
After consideration, Emperor Xuanzong of Tang decided to accept Yang Guozhong's suggestion. He summoned Chen Xuanli, the Grand General of the Dragon Guard, to the palace and ordered him to secretly assemble the soldiers while also preparing over nine hundred fine horses. Chen Xuanli, being the emperor's trusted confidant, quickly completed the task.
Early the next morning, Emperor Xuanzong of Tang led Yang Guifei, Lady of Qin, Lady of Han, Lady of Guo, along with some imperial princes, grandsons, and palace concubines, as well as ministers Yang Guozhong and Chen Xuanli, as they rode out of the western gate of the capital by carriage, fleeing westward. After escaping from Chang'an, the emperor ordered messengers to notify the prefectures and counties along the way to prepare for his arrival. However, many local officials had already fled, and the imperial entourage traveled for a long time without finding a place to eat. The accompanying eunuchs begged some coarse food from the local people, which was mixed with beans and wheat. The princes and grandsons, who had always lived a life of luxury and fine dining, had never eaten such food, but they were so ravenously hungry that they ate it anyway. Without bowls or chopsticks, they used their hands to scoop up the food, and in no time at all, they had devoured every last bit of the coarse meal the people had given them.
Emperor Xuanzong of Tang, while eating a steamed bun, thought about his predicament and couldn't help but shed tears. An elderly man approached him and said, "Years ago, An Lushan's ambition to rebel was already very obvious. Many people reported it to the court, but instead of investigating, the court imprisoned or killed those who reported it. The ministers around Your Majesty, apart from flattery, had no real abilities. With such people by your side, Your Majesty was completely unaware of the outside world. We common people had long foreseen that this day would come. But what use was knowing? If Your Majesty hadn't fled here, how could we have ever seen you?" Emperor Xuanzong said dejectedly, "Today's situation is entirely my own doing, and now I am filled with regret that is too late to remedy."
Emperor Xuanzong of Tang and his entourage, after a long and arduous journey, arrived at Mawei Post Station (west of present-day Xingping, Shaanxi). The accompanying soldiers, having traveled a great distance, were utterly exhausted and had gone without food for a long time, so they were all furious, thinking that Emperor Xuanzong should have stayed safely in Chang'an instead of fleeing everywhere, forcing them to endure such hardships. They believed that their suffering was entirely caused by the treacherous Prime Minister Yang Guozhong, and thus they resolved to kill him to vent their anger.
Just at this moment, Yang Guozhong was surrounded on horseback by more than twenty Tibetan envoys who were demanding grain from him. When some soldiers saw this scene, they immediately shouted loudly, "Yang Guozhong is colluding with the barbarians—he is plotting a rebellion!" Hearing this, the other soldiers became even more enraged and began shooting arrows at Yang Guozhong. Realizing the situation was dire, Yang Guozhong quickly spurred his horse and fled toward the west gate. The soldiers chased after him relentlessly and soon surrounded him completely. Without any discussion, they thrust their long spears at Yang Guozhong. In no time, Yang Guozhong was dead. Several soldiers cut off his head and placed it in front of the post station where Emperor Xuanzong of Tang was staying, then surrounded the station. Hearing the commotion outside, Emperor Xuanzong sent someone to investigate. A eunuch returned and reported to him, "The soldiers have killed Yang Guozhong and are surrounding the post station, refusing to leave." Leaning on his staff, Emperor Xuanzong slowly walked out of the station and urged the troops to return to their camps and rest. But the soldiers were too agitated and paid no attention to his words. Emperor Xuanzong then sent for Chen Xuanli and asked him why the soldiers would not disperse. Chen Xuanli replied, "Yang Guozhong colluded with the barbarians and plotted rebellion, so the soldiers have already killed him. Yang Guifei is his cousin. If the Emperor allows her to remain by his side, it would be unjustifiable both in reason and sentiment. Therefore, I beg Your Majesty to order her execution." Yang Guifei was the beloved consort of Emperor Xuanzong—how could he bear to have her killed?

Consort Yang, originally named Yang Yuhuan, was officially designated as the Imperial Consort by Emperor Xuanzong of the Tang Dynasty when she was twenty-seven years old. Not only was she exceptionally beautiful, but she was also skilled in singing and dancing, which deeply endeared her to the emperor. Emperor Xuanzong even had a special bathing pool, the Haitang Pool, built exclusively for her, a privilege unmatched among the numerous concubines in the imperial harem. Neglecting state affairs, the emperor spent his days feasting and reveling with Consort Yang, and he appointed her cousin Yang Guozhong as Prime Minister while granting her three sisters the title of State Ladies.
Looking at the enraged soldiers, Emperor Xuanzong of Tang was in a state of utter confusion. He thought for a moment and slowly replied, "You should go back first; I will make sure you are satisfied." After saying this, he leaned on his cane and walked slowly back to the inn. An official named Wei E said to him, "The situation is extremely critical now. If Your Majesty cannot make an immediate decision, the consequences may be unimaginable!" Emperor Xuanzong replied, "Although Yang Guifei is the cousin of Yang Guozhong, she has always lived deep within the palace. How could she be involved in Yang Guozhong's rebellion?"
The eunuch Gao Lishi said, "The soldiers have already killed Yang Guozhong. Although the Imperial Consort has committed no crime, if she continues to remain by Your Majesty's side, how can the soldiers feel at ease? Your Majesty, please consider this carefully: only when the soldiers are at ease will they serve the country, and only then can Your Majesty be safe." Emperor Xuanzong of Tang, seeing that there was no way to save Yang Guifei's life, sent Gao Lishi to take her elsewhere to be executed. Upon hearing the news that Yang Guifei had been executed, the soldiers' agitated emotions calmed down, and they returned to their camp.
After this, Emperor Xuanzong of Tang left Crown Prince Li Heng behind to oversee state affairs while he himself fled in haste toward Chengdu. Li Heng traveled north from Mawei Post and ascended the throne as emperor at Lingwu (southwest of present-day Lingwu, Ningxia). Li Heng became Emperor Suzong of Tang.