Lwt: pause versus yield

It seems Lwt.pause and Lwt_{unix/main}.yield share almost the same functionality, where pause works for any backend. I thought yield exists only for backward compatibility, but it seems yield is not simply defined as pause in the current codebase. Why do we need two separate pools? What could be an issue if let yield = pause? Relatedly, from the user perspective, what are the pitfalls of using pause instead of yield (or vice versa)?

5 Likes

You don’t need two separate pools. The docs of yield recommend using pause instead, so new code should be using only that one pool.

Since yield and pause used different pools for a long time, we are (I was?) concerned that some existing code might rely, perhaps implicitly, on the relative ordering of fulfillment of promises from yield and pause, i.e. effectively assuming that the pools are indeed different, and resolved in separate “phases.” So, to avoid the risk of unnecessary breakage by subtly changing the relative order, since the Lwt-side code is not complex, the pools remain separate.

No real pitfalls — just that you shouldn’t use yield at all, and if your code (including libraries you are using) does end up using a mixure of yield and pause and somehow depend on the promises from each being resolved in different “phases,” your code might break if Lwt ever does decide to unify the pools for some reason.

3 Likes

Thanks. Are there plans to @deprecate the function yield and its friends in the near future?

I am not personally aware of any such plans. However, I suggest asking about that in the repo, since that will ping the maintainers and others most directly interested in the details of Lwt’s own code :slight_smile: