[Bug]: ResizeHandler re-attaches a destroyed engine's ResizeObserver when a re-init happens before its first frame
Variants: embla-carousel (Core), embla-carousel-react
Browsers: Chrome (the bug is in the core logic, not browser-specific)
Version: 8.6.0 — same code in 9.0.0-rc03 and on master (85a5e3c)
Reproduction: the self-contained HTML page below (Embla 8.6.0 from unpkg, no build step)
Steps to reproduce
- Create a carousel and trigger any re-init before the next animation frame: call
emblaApi.reInit()right after creating it, change the slide list right after a resize-driven re-init, or let abreakpointsmedia query change fire while the window is being dragged. - Wait two frames.
- Count
observe()calls on ResizeObservers that were already disconnected, and how oftenwatchResizeruns for a single container resize.
ResizeHandler.init creates the observer at once but only calls observe() in a requestAnimationFrame callback (ResizeHandler.ts#L29-L43):
resizeObserver = new ownerWindow.ResizeObserver(onResize)
ownerWindow.requestAnimationFrame(() => {
observeNodes.forEach((node) => resizeObserver.observe(node))
})When the engine is destroyed before that frame (reInit() → deActivate() → resizeHandler.destroy()), destroy() sets destroyed = true and disconnects the observer. The pending frame then calls observe() on that disconnected observer anyway, which attaches it again. Nothing disconnects it after that.
Minimal reproduction (plain HTML, Embla from unpkg):
repro.html<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<style>
.embla { overflow: hidden; width: 900px; }
.embla__container { display: flex; }
.embla__slide { flex: 0 0 33%; min-width: 0; height: 120px; margin-right: 8px; background: #ddd; }
</style>
</head>
<body>
<div class="embla">
<div class="embla__container">
<div class="embla__slide">1</div>
<div class="embla__slide">2</div>
<div class="embla__slide">3</div>
<div class="embla__slide">4</div>
</div>
</div>
<pre id="log"></pre>
<script>
// Count observe() calls on ResizeObservers that were already disconnected.
const disconnected = new WeakSet()
let reattached = 0
const { observe, disconnect } = ResizeObserver.prototype
ResizeObserver.prototype.disconnect = function () {
disconnected.add(this)
return disconnect.call(this)
}
ResizeObserver.prototype.observe = function (...args) {
if (disconnected.has(this)) reattached++
return observe.apply(this, args)
}
</script>
<script src="https://unpkg.com/[email protected]/embla-carousel.umd.js"></script>
<script>
const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve))
const log = (line) => (document.getElementById('log').textContent += line + '\n')
let watchResizeCalls = 0
const emblaApi = EmblaCarousel(document.querySelector('.embla'), {
// Count calls only; returning false keeps Embla from re-initialising on the resize.
watchResize: () => {
watchResizeCalls++
return false
}
})
// Any re-init before the next frame, e.g. the slide list changing right after init
// or right after a resize-driven reInit().
emblaApi.reInit()
;(async () => {
await nextFrame()
await nextFrame()
log(`observe() calls on disconnected ResizeObservers: ${reattached} (expected 0)`)
watchResizeCalls = 0
document.querySelector('.embla').style.width = '600px'
await nextFrame()
await nextFrame()
log(`watchResize calls for one container resize: ${watchResizeCalls} (expected 1)`)
})()
</script>
</body>
</html>Output in Chrome with 8.6.0:
observe() calls on disconnected ResizeObservers: 5 (expected 0)
watchResize calls for one container resize: 2 (expected 1)The 5 calls are the first engine's observer re-attaching to the container and the 4 slides.
Expected behavior
A ResizeObserver that destroy() disconnected stays disconnected: observe() is never called on it, and watchResize runs once per resize, from the live engine's observer.
Additional context
How we hit it. In a real app it happens without calling reInit() by hand:
- The slide list changes right after a resize-driven re-init, so the
SlidesHandlerre-init runs before theResizeHandlerframe. - A
breakpointsmedia query changes while the window is dragged. Media querychangeevents are dispatched before animation-frame callbacks in the same frame. If the previous frame's resize re-initialised the carousel, its observer has not observed yet when the breakpoint re-init destroys it. A 1100 ↔ 900 px drag across a single(min-width: 1000px)breakpoint left two stale observers.
Impact.
- Each stale observer keeps the old container and slide nodes referenced for the carousel's lifetime.
- Its callback calls the user's
watchResizewith the liveemblaApi, and it does so before the live observer, because the stale one was created first.defaultCallbackthen bails out ondestroyed. - A
watchResizethat keeps state across calls, such as skipping unchanged widths, sees each resize twice. The stale call consumes the change, the live observer then sees nothing new, and the carousel keeps the previous width's snaps and limits. We worked around it on our side, but the stale observers remain. - In 9.0.0-rc03,
onResizeemits theresizeevent before it checksdestroyed, so a stale observer firesresizelisteners as well.
Suggested fix. Skip the deferred observe() once the handler is destroyed. With this one-line change, the reproduction prints 0 and 1:
resizeObserver = new ownerWindow.ResizeObserver(onResize)
ownerWindow.requestAnimationFrame(() => {
+ if (destroyed) return
observeNodes.forEach((node) => resizeObserver.observe(node))
})Alternatively, destroy() could cancel the frame with cancelAnimationFrame. Happy to open a PR.
#1383 also changes ResizeHandler, but it only defers the reInit() inside onResize and leaves this observe() frame as it is.
Source: davidjerleke/embla-carousel