Just this weekend past, I experienced a similar problem that you described on my M40 32hp Universal.
Fault finding was immediately centered on the fuel system. After checking draining the water sep bowl on the Racor R24S fuel filter I noted that the fuel lift pump was not operating.
I replaced the fuel lift pump and although engine would start and run, the same issue as previous ensued.
I then found that the 12VDC elec spade fitting (supply to lift pump) was intermittent. After this was made good - stripped and new terminal ends, fuel lift pump operates and air was then heard being sucked in (engine not running) and after close inspection, air (bubbles) were being drawn into the Racor water sep bowl through the drain that had been used to drain the bowl earlier. Bugger!
So it appears that my original issue was bad 12V supply to fuel lift pump. Nothing wrong with the fitted pump - now have a spare
.
But in the course of draining the Racor fuel bowl, it did not seal shut properly, no fuel leaking out, but allowing air to be sucked in when fuel pump operating correctly.
So although the engine would run, it was a little unsteady at idle. Which brings me back to a couple of the earlier posts - Christian/Juice/Loren about replacing items in the fuel system.
If you have a fuel lift pump fitted, ensure that it is operating correctly. Ensure that all hoses and filters etc. are tight and no air being drawn into the fuel supply.
Good luck and get that filter sooner rather than later.
Good practice is to change fuel filters annually for reliability, more regularly if fuel is dirty or not used for a few years. And good idea to always keep a spare primary and secondary - if fitted, fuel filter onboard. A manufacturer's note that came with the fuel lift pump I purchased reads, ensure 75 micron in line filter be fitted on suction side of fuel lift pump.
Hammy in sunny Bermuda
Merrimist 38-200