Separate list of abbreviations/acronyms for each chapter in book with glossaries package

I'm trying to generate a list of abbreviations/acronyms separately for each chapter in a book using the glossaries package and xindy. Any abbreviation can appear in any chapter.

Using Nicola Talbot's suggestions on how this works for chapters and sections as a starting point, I cannot make this work for more than two chapters.

The following MWE does not give the correct output: in Chapter 2, mass is not listed in the glossary, even though \gls{m} is used in the equation. The expected behaviour would be for mass to be included in the glossary.

\documentclass{report}
\usepackage{datatool-base}
\usepackage[counter=chapter,xindy,section=section]{glossaries}
\GlsSetXdyMinRangeLength{0}
\makeglossaries
\newglossaryentry{E}{name={\ensuremath{E}},description={energy}}
\newglossaryentry{m}{name={\ensuremath{m}},description={mass}}
\newglossaryentry{c}{name={\ensuremath{c}},description={speed of light}}
\newglossaryentry{v}{name={\ensuremath{v}},description=velocity}
\newglossarystyle{mystyle}%
{%
\setglossarystyle{list}%
\renewcommand*{\glossaryentrynumbers}[1]{\striprelax##1\endstriprelax}%
\renewcommand*{\glsXchapterXglsnumberformat}[2]{##2}%
\renewcommand*{\delimR}{,}%
\renewcommand*{\glossentry}[2]{%
\edef\doifinlocation{\noexpand\ifinlocation{\thechapter}{##2}}%
\doifinlocation
{%
\item \glossentryname{##1} \glossentrydesc{##1}%
}%
}%
}
\newcommand{\ifinlocation}[3]{%
\DTLifinlist{#1}{#2}{#3}{}%
}
\def\striprelax\relax#1\endstriprelax{#1}
\setglossarystyle{mystyle}

\begin{document}

\chapter{Sample Chapter}
\printglossary
$$\gls{E} = \gls{m}\cdot \gls{c}^2$$
\glsresetall
\chapter{Another Chapter (2)}
\printglossary
$$\gls{E} = \frac{\gls{m}\gls{v}^2}{2}$$

\glsresetall
\chapter{Yet another Chapter (3)}
\printglossary
$$\gls{m}\gls{v}$$

\end{document}


If \gls{m} is removed from Chapter 3, the list of abbreviations appears correctly for Chapter 2.

$$\gls{v}$$


The difference between the outputs appears to originate in the .gls file. In the MWE's .gls file, the relevant lines are:

\glossentry{m}{\glossaryentrynumbers{\relax
\glsXchapterXglsnumberformat{}{1}\delimR
\glsXchapterXglsnumberformat{}{3}}}\glsgroupskip


Changing this manually to include chapter 2 and re-running LaTeX on the MWE creates the right result.

\glossentry{m}{\glossaryentrynumbers{\relax
\glsXchapterXglsnumberformat{}{1}\delimR
\glsXchapterXglsnumberformat{}{2}\delimR
\glsXchapterXglsnumberformat{}{3}}}\glsgroupskip


How can the correct output be generated automatically rather than by messing with the .gls file?