TokenTextSplitter hangs forever with chunk_overlap == chunk_size (non-termination + unbounded memory)
Self Checks
- [X] This is a bug report, not a feature request
- [X] I have searched the existing issues
Description
split_text_on_tokens (api/core/rag/splitter/text_splitter.py) never terminates when chunk_overlap >= tokens_per_chunk: the window advance is tokens_per_chunk - chunk_overlap, which is <= 0, so start_idx never moves and the loop spins forever - the process hangs and memory grows without bound.
TokenTextSplitter.__init__ validates only chunk_overlap > chunk_size (strict), so the equality case (chunk_overlap == chunk_size) passes validation and hits the non-terminating loop.
Reproduction
from core.rag.splitter.text_splitter import TokenTextSplitter
splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=10) # passes validation
splitter.split_text("hello world " * 50) # never returns; memory grows without bound
Verified on current main: the call is still running after 6+ seconds on a 600-char input (thread-join probe); any larger input drives unbounded memory growth. Also applies to tokens_per_chunk <= 0.
Expected: reject the configuration (as chunk_overlap > chunk_size already is) before looping. Actual: hang + OOM.
Environment
- Dify main @ (current)
- Any platform
One additional scope detail: guarding only TokenTextSplitter.__init__ would leave direct calls to split_text_on_tokens unprotected. Tokenizer is a dataclass without validation, and the helper accepts it directly. The helper also has no exit when the first chunk reaches the end, so equality causes non-termination even for nonempty input shorter than one chunk. [1]
For regression coverage, TestSplitTextOnTokens already uses mock encoders/decoders, so the boundary cases can be tested without tiktoken. Those tests currently cover valid configurations only. Suggested coverage is rejection of tokens_per_chunk <= 0 and chunk_overlap >= tokens_per_chunk at the helper boundary, plus constructor rejection of equality. Until corrected, use a positive chunk size with 0 <= chunk_overlap < chunk_size. [2] [3] [4]
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
Hi @simpleqt, thanks for opening this issue.
Why this is being closed
The minimal splitter example is useful, but the report says only current main and any platform; it does not identify the exact tested commit or whether this was a source-only test or a deployed workflow. Those details are needed to independently verify the affected setup. The bug report template asks for version, deployment and reproduction context. This closure does not mean the underlying observation is incorrect.
Next steps
Please provide the exact tested commit or release, the environment or explicitly state that the reproduction is source-only, and the test command and observed output. Run any hang reproduction with an external timeout or isolated process. Once the context is available, please request reopening so the report and linked fix can be reviewed.