Having trouble shutting down a stream in a timely manner

I’ve written some custom GraphStages and they work well, but they do not shut down in a timely manner when I use a killswitch or implement an asynchronous sidechannel with a promise. Here’s the code for the graph stages:


and the shape of my graph is val graph = source3.viaMat(KillSwitches.single)(Keep.right).via(readerFlow).via(uploaderFlow).toMat(sink)(Keep.both)

When I run this graph, and initiate the kill switch, the stream seems to want to complete 8 elements from the source before halting execution and cleaning up. I need much more responsive shutdown times though. Any suggestions on what I’m doing wrong here? Am I relying on backpressure too much or something?

I managed to sidestep this issue by changing my approach. I simplified my graph stages and use flatMapMerge so that a new substream would be launched for each file in my stream. The only (mild) problem now is that flatmapmerge only seems to allow for my subgraph to have one output. I’ve resolved the problem by having my subgraph hook into a merge hub or two to provide multiple data exit points, but it’d be nice if there was a way to do it via normal flatmapmerge.