GANSU  Returning the fueling nozzle to the pump, Zhang Li jumped into the driver’s seat of his gas-guzzling Land Rover. “Such a long line,” said the 45-year-old tour guide, shaking his head. “What’s the world coming to? My stocks are worth air, and now I have to wait an hour for overpriced gas, too.”

Mr. Zhang’s lament elegantly captured the twin dilemmas that led China’s leaders to unexpectedly raise gas prices here by double-digit percentages Thursday. While much has been said about China’s hesitance to raise domestic prices for fear of inflation, Beijing was facing an equally vexing problem: artificially low gas and diesel prices were indirectly depressing China’s stock markets by hurting the performance of the energy giants Sinopec and PetroChina.

Price controls have saved Chinese consumers and businesses billions  and prevented the kind of protests that led to rioting, and even a murder at the pump, the last time prices rose.

But controls have also squeezed Sinopec and PetroChina, China’s top oil refiner and producer, respectively. They still had to pay near-record oil prices on world markets even if they were not allowed to charge market prices to consumers. The companies lost money on every gallon of gas they sold in China.