Skip to content

MySqlBulkCopy: MySqlConnector.MySqlException: 'Error during LOAD DATA LOCAL INFILE' #974

Closed

Description

@RobertGlynWilliams

Hi,

Thank you for this great library! ;)

I periodically get exceptions from the MySqlBulkCopy class.
This happens when the utf8 encoder tries to encode a unicode character into a buffer with a length of 1.

Exception:
The output byte buffer is too small to contain the encoded data, encoding 'Unicode (UTF-8)' fallback 'System.Text.EncoderReplacementFallback'. (Parameter 'bytes')

Example code to reproduce:

static async Task Main(string[] args)
{
    /*
    CREATE TABLE `testtbl` (
	    `a` MEDIUMTEXT NOT NULL,
	    `b` VARCHAR(255) NOT NULL
    )
     */

    var a = new StringBuilder();
    for (int i = 0; i < 1048573; i++)
    {
        a.Append("a");
    }

    var b = "Å string that crashes application";

    var dataTable = new DataTable();
    dataTable.Columns.Add("a");
    dataTable.Columns.Add("b");
    dataTable.Rows.Add(new object[] { a, b });

    using var connection = new MySqlConnection("...;AllowLoadLocalInfile=true");
    await connection.OpenAsync();
    var bulkCopy = new MySqlBulkCopy(connection);
    bulkCopy.DestinationTableName = "testtbl";
    await bulkCopy.WriteToServerAsync(dataTable);
}

Activity

  1. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    Thanks for the great repro!

  2. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    I naively assumed that Convert would output the leading bytes and maintain internal state to output the rest of the UTF-8 bytes in the next block. (And apparently I wasn't the only one: npgsql/npgsql#2849.)

    The fix will be to call GetByteCount to ensure that at least one char can be converted.

  3. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    Even worse, I already made this mistake but didn't learn from it ☹ #520.

  4. added a commit that references this issue on Apr 23, 2021
    ba778fa
  5. RobertGlynWilliams commented on Apr 23, 2021

    @RobertGlynWilliams
    Author

    Thank you for the bugfix 💯 .

  6. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    Fixed in 1.3.5.

  7. chrisdcmoore commented on May 14, 2021

    @chrisdcmoore

    Hey @bgrainger - I noticed this issue referenced an issue I opened and fixed in Npgsql a while back, and I just wanted to give you a heads up that I've discovered the fix I wrote doesn't appear to be entirely correct - I've opened another issue against Npgsql describing what I think is going on over there, so you may wish to check whether or not it also affects your fix to this issue here!

    npgsql/npgsql#3733

    Cheers

  8. bgrainger commented on May 14, 2021

    @bgrainger
    Member

    Thanks @chrisdcmoore; it looks like my fix is subject to this same problem. I appreciate your bringing it to my attention.

  9. added a commit that references this issue on May 14, 2021
    4a5448f
  10. bgrainger commented on Jun 16, 2021

    @bgrainger
    Member

    Improved fix in 1.3.9.

  11. added theissue type on Jan 18, 2026
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions