By Sebastian Pellejero

Oil prices swung between small gains and losses Tuesday, extending a recent stall around $40 a barrel ahead of this week's data on U.S. crude stockpiles.

U.S. crude futures for August delivery recently were down 0.1% at $40.59 a barrel on the New York Mercantile Exchange.

The pause came ahead of data on weekly U.S. crude stockpiles from the trade group American Petroleum Institute due out late afternoon and Wednesday's Energy Information Administration report. Last week's data showed declines in U.S. crude inventories, suggesting the U.S. economy was in recovery mode and oil demand was rebounding.

Concerns that the rise in new coronavirus cases will hamper the recovery in fuel demand have weighed on oil prices in recent weeks. Among other concerns, the health crisis has cast doubt on whether China, the world's second largest oil consumer, will reach its targets to buy U.S. energy. The latest data on U.S. exports in May show China has purchased just $2 billion of a planned $25 billion worth of crude and other products this year.

Demand concerns are being weighed against supply cuts. The Organization of the Petroleum Exporting Countries and its allies have undertaken record production cuts to help buoy crude prices. Saudi Arabian Oil Co., or Aramco, the nation's oil giant, raised its crude prices for August for a third consecutive month. Analysts say that could encourage inventory purchases, which would support oil prices broadly.

In the U.S., crude supply is falling at its quickest pace ever, adding to the recovery in prices. Weekly output recently fell to 10.5 million barrels a day, down from 13 million barrels in late March, government data show. Yet for beleaguered shale producers such as Chevron Corp. and Continental Resources Inc., oil around $40 a barrel isn't enough to help ease the strain of debt they took during boom times.

As crude trades near the top of its recent range, market watchers are starting to expect a slight increase in shale output and the tapering of production cuts, TD Securities analysts said in a note. That could limit oil's gains.

Brent crude futures for September delivery, the global gauge of oil prices, extended Monday's gains, rising 0.3% to $43.24 a barrel on the Intercontinental Exchange Tuesday.

Elsewhere in commodities, natural gas futures for August delivery continued to surge Tuesday. Prices have bounced back from a 25-year low reached late last month, but analysts and traders are questioning how much higher they can go, since there is too much supply of the power-generation and heating fuel.

Write to Sebastian Pellejero at sebastian.pellejero@wsj.com