Skip to content

"\x89" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) #55

Open

Description

@tdegrunt

Hi,

I've checked out this repository and wrote the below simple test.rb, while using the below image:

require 'mongoid-grid_fs'

Mongoid.configure do |config|
  config.connect_to('mongoid-grid_fs_test')
end

file = File.open('./signature.png')
grid_file = Mongoid::GridFs.put(file.path)

If I execute this with bundle exec ruby test.rb, I'm getting the following exception.
Am I doing something wrong or overlooking something?

❯ bundle exec ruby test.rb
/usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/binary.rb:137:in `encode': "\x89" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/binary.rb:137:in `block in to_bson'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/encodable.rb:81:in `encode_binary_data_with_placeholder'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/binary.rb:134:in `to_bson'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/hash.rb:46:in `block (2 levels) in to_bson'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/hash.rb:43:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/hash.rb:43:in `block in to_bson'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
    from /usr/local/lib/ruby/gems/2.1.0/gems/bson-3.1.2/lib/bson/hash.rb:42:in `to_bson'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/protocol/message.rb:166:in `block in serialize_documents'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/protocol/message.rb:165:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/protocol/message.rb:165:in `serialize_documents'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/protocol/message.rb:323:in `serialize'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/connection.rb:171:in `block in write'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/connection.rb:169:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/connection.rb:169:in `write'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:590:in `block (2 levels) in flush'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:182:in `block in ensure_connected'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:115:in `block in connection'
    from /usr/local/lib/ruby/gems/2.1.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:64:in `block (2 levels) in with'
    from /usr/local/lib/ruby/gems/2.1.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `handle_interrupt'
    from /usr/local/lib/ruby/gems/2.1.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `block in with'
    from /usr/local/lib/ruby/gems/2.1.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `handle_interrupt'
    from /usr/local/lib/ruby/gems/2.1.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `with'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:114:in `connection'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:178:in `ensure_connected'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:589:in `block in flush'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:617:in `block in logging'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:164:in `block in instrument'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:164:in `instrument'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/instrumentable.rb:31:in `instrument'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:616:in `logging'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:587:in `flush'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:358:in `pipeline'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/operation/write.rb:47:in `execute'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:665:in `write'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:273:in `insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/collection.rb:128:in `block (2 levels) in insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/cluster.rb:249:in `block in with_primary'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:204:in `block in ensure_primary'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/executable.rb:25:in `execute'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/node.rb:203:in `ensure_primary'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/cluster.rb:248:in `with_primary'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/collection.rb:127:in `block in insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/retryable.rb:30:in `call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/retryable.rb:30:in `with_retry'
    from /usr/local/lib/ruby/gems/2.1.0/gems/moped-2.0.6/lib/moped/collection.rb:125:in `insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:117:in `insert_with_clear_cache'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:79:in `insert_as_root'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:27:in `block in insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:118:in `block (2 levels) in prepare_insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:84:in `run_callbacks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/interceptable.rb:138:in `run_callbacks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:117:in `block in prepare_insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:84:in `run_callbacks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/interceptable.rb:138:in `run_callbacks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:23:in `insert'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/savable.rb:23:in `save'
    from /usr/local/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/savable.rb:44:in `save!'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:154:in `block (2 levels) in put'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:479:in `call'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:479:in `chunking'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:147:in `block in put'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:458:in `call'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:458:in `block in reading'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:457:in `open'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:457:in `reading'
    from /Users/tdegrunt/Development/Personal/mongoid-grid_fs/lib/mongoid/grid_fs.rb:136:in `put'
    from (eval):2:in `put'
    from test.rb:8:in `<main>'

Here's the output of my bash locale:

❯ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

The image used (via wikipedia):
signature

For completeness here's the output of bundle list:

~/Development/Personal/mongoid-grid_fs master*
❯ bundle list
Gems included by the bundle:
  * actionmailer (4.2.3)
  * actionpack (4.2.3)
  * actionview (4.2.3)
  * activejob (4.2.3)
  * activemodel (4.2.3)
  * activerecord (4.2.3)
  * activesupport (4.2.3)
  * arel (6.0.2)
  * bson (3.1.2)
  * builder (3.2.2)
  * bundler (1.10.5)
  * coderay (1.1.0)
  * connection_pool (2.2.0)
  * erubis (2.7.0)
  * globalid (0.3.5)
  * i18n (0.7.0)
  * json (1.8.3)
  * loofah (2.0.2)
  * mail (2.6.3)
  * method_source (0.8.2)
  * mime-types (2.6.1)
  * mini_portile (0.6.2)
  * minitest (5.7.0)
  * mongoid (4.0.2)
  * mongoid-grid_fs (2.1.0)
  * moped (2.0.6)
  * nokogiri (1.6.6.2)
  * optionable (0.2.0)
  * origin (2.1.1)
  * pry (0.10.1)
  * rack (1.6.4)
  * rack-test (0.6.3)
  * rails (4.2.3)
  * rails-deprecated_sanitizer (1.0.3)
  * rails-dom-testing (1.0.6)
  * rails-html-sanitizer (1.0.2)
  * railties (4.2.3)
  * rake (10.4.2)
  * slop (3.6.0)
  * sprockets (3.2.0)
  * sprockets-rails (2.3.2)
  * thor (0.19.1)
  * thread_safe (0.3.5)
  * tzinfo (1.2.2)

Activity

  1. Titinux commented on Jul 16, 2015

    @Titinux

    Same problem here but if I downgrade bson to 3.1.1 no problem.

  2. pezholio commented on Aug 28, 2015

    @pezholio

    Yup, same here. Pinning to 3.1.1 fixes this, because 3.1.2 encodes to UTF-8 and then switches to binary.

  3. dblock commented on Oct 18, 2026

    @dblock
    Contributor

    Follow-up comments identify a BSON 3.1.2 regression (3.1.1 worked). #89 adds binary pathname-upload coverage with PNG-header/non-UTF8 bytes across multiple chunks, passing on the current stack. This is not proof that this gem fixed the historical dependency regression; leaving this open pending legacy-specific evidence.

    Posted by an AI on behalf of @dblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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