Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job batch doesn't work properly with PostgreSQL #36061

Closed
raftx24 opened this issue Jan 27, 2021 · 3 comments · Fixed by #36081
Closed

Job batch doesn't work properly with PostgreSQL #36061

raftx24 opened this issue Jan 27, 2021 · 3 comments · Fixed by #36081

Comments

@raftx24
Copy link
Contributor

raftx24 commented Jan 27, 2021

  • Laravel Version: 8.24.0
  • PHP Version: 7.4.13
  • Database Driver & Version: PostgreSQL 13.0

Description:

A serialized object in PHP that has null values before and after private fields like will fail to serialize because Postgres PDO serialization will stop on null values.
Example:
"O:1:"A":1:{s:8:"\0A\0field";N;}"

This is because of PQescapeStringConn

A terminating zero byte is not required, and should not be counted in length. (If a terminating zero byte is found before length bytes are processed, PQescapeStringConn stops at the zero; the behavior is thus rather like strncpy.)

Therefore, if we need to store a serialized object into a text Postgres column, it will fail.

Laravel uses serialization for job_batches.options and when used with PostgresSQL causes this error:

 ErrorException 

  Insufficient data for unserializing - 273 required, 90 present

  at vendor/laravel/framework/src/Illuminate/Bus/DatabaseBatchRepository.php:286
    282▕             (int) $batch->total_jobs,
    283▕             (int) $batch->pending_jobs,
    284▕             (int) $batch->failed_jobs,
    285▕             json_decode($batch->failed_job_ids, true),
  ➜ 286▕             unserialize($batch->options),
    287CarbonImmutable::createFromTimestamp($batch->created_at),
    288$batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at) : $batch->cancelled_at,
    289$batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at) : $batch->finished_at
    290▕         );

      +4 vendor frames 

Steps To Reproduce:

class A {
    private $field;
}

$a = new A();

Bus::batch([])
    ->then(fn ($batch) => $a)
    ->dispatch();

Possible Fix

We can use base64_encode before inserting & base64_decode after fetching the serialized objects.

If you find this solution ok let me know and I'll create a PR for it.

Thank you.

References:
PostgreSQL: Chapter 33. libpq — C Library
Text column type doesn't reliably hold serialized variables

@staudenmeir
Copy link
Contributor

staudenmeir commented Jan 27, 2021

This is the approach we are using to fix the issue in the cache component: #25530

Can we it reuse it somehow?

@taylorotwell
Copy link
Member

I would be fine with that solution as long as we can do it in a backwards compatible way for other database drivers. Or, ideally, not base64_encode at all when using other drivers.

@raftx24
Copy link
Contributor Author

raftx24 commented Jan 28, 2021

I'll create a PR that will do base64_encode only for PostgreSQL

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

Successfully merging a pull request may close this issue.

4 participants