Skip to content

BeanstalkError: EXPECTED_CRLF when putting UTF-8 string containing non-ASCII characters #22

Description

@egorgvo

Description

When calling:

client.put_job_into(tube_name, "café (coffee)")

I get the following traceback:

Traceback (most recent call last):
  File "/media/user/code/module.py", line 49, in push_job
    return self.put_job(  # type: ignore[no-any-return]
  File "/media/user/venv/lib/python3.10/site-packages/pystalk/client.py", line 300, in put_job
    return self._receive_id(socket)
  File "/media/user/venv/lib/python3.10/site-packages/pystalk/client.py", line 221, in _receive_id
    status, gid = self._receive_name(sock)
  File "/media/user/venv/lib/python3.10/site-packages/pystalk/client.py", line 230, in _receive_name
    raise BeanstalkError(message.rstrip())
pystalk.client.BeanstalkError: b'EXPECTED_CRLF'

This happens because the length of the string in bytes may differ from the number of characters (len(data) vs len(data.encode('utf-8'))) when the string contains non-ASCII characters like é.


Suggested Fix

Update put_job to encode data to bytes before calculating the length (see comment inside the code):

def put_job(self, data: Union[str, bytes], pri: int = 65536, delay: int = 0, ttr: int = 120):
    with self._sock_ctx() as socket:
        if not isinstance(data, bytes):  # <-- I just moved this check from above to here
            data = data.encode('utf-8')
        message = 'put {pri} {delay} {ttr} {datalen}\r\n'.format(
            pri=pri, delay=delay, ttr=ttr, datalen=len(data)
        ).encode('utf-8')
        message += data
        message += b'\r\n'
        self._send_message(message, socket)
        return self._receive_id(socket)

This ensures the correct byte length is used in the put command, preventing EXPECTED_CRLF errors with UTF-8 strings.

If needed, I can prepare a PR with this fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions