Vim.loop.spawn leaves subprocess running after exit

I have written the following code to automatically change my neovim theme when the color-scheme changes in gsettings:

;(function()
	local stdout = vim.loop.new_pipe(false)
	vim.loop.spawn(
		'gsettings',
		{
			args = {'monitor', 'org.gnome.desktop.interface', 'color-scheme'},
			stdio = {nil, stdout, nil},
		}
	)
	vim.loop.read_start(stdout, function (_, s)
		vim.schedule(function()
			s = string.gsub(s, '^%s+', '')
			s = string.gsub(s, '%s+$', '')
			s = string.gsub(s, '[\n\r]+', ' ')

			s = string.match(s, 'color%-scheme: (.*)')
			set_bg(s)
		end)
	end)
end)()

This works, but I noticed that neovim leaves the gsettings process running after exiting. How can I make neovim properly terminate the subprocess?

My neovim tells me to use uv.spawn instead of loop.spawn, but in any case, you should be able to specify an on_exit handler as the third argument.

Actually, if the process it not terminating, I suppose on_exit will not be much help. In this case, you could keep the return values of spawn instead, which should include the process id, and then kill the process with uv.kill().