The overlay is only set to the caret position when the caret is redrawn (via redrawTask.trigger()). If setOverlayElement is called but the caret has no pending draw request, the overlay won't be aligned to the caret redraw. I think you should add:
```
shouldUpdateCaretSize = true;
redrawTask.trigger();
```
After setting the overlay to ensure it is relocated to the right position asap.