Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
88 stars 33 forks source link

Bug + fix: Site crashes with out of memory error when adding a downloadable product and hitting Update button #536

Open lordspace opened 11 months ago

lordspace commented 11 months ago

Describe the bug

Site crashes with out of memory error when adding a downloadable product and hitting Update button This happens with the latest version of the Woo Subscriptions which is v5.6.0

To Reproduce

You need one subscription product with 100s of downloadable products and lots of orders.

wp db query --debug 'SELECT count(download_id) from some_prefix_woocommerce_downloadable_product_permissions WHERE product_id = 20901'

SELECT order_id, download_id from some_prefix_woocommerce_downloadable_product_permissions WHERE product_id = 20901

+--------------------+ | count(download_id) | +--------------------+ | 2319695 | +--------------------+

Expected behavior

Not to crash due to memory limits.

Actual behavior

This function is the cause of the error as it puts everything in the memory. The function returned 2+ million rows. grant_new_file_product_permissions( $product_id, $variation_id, $downloadable_files )

/Automattic/woocommerce-subscriptions-core/blob/trunk/includes/class-wcs-download-handler.php#L232C25-L232C59

](https://github.com/Automattic/woocommerce-subscriptions-core/blob/trunk/includes/class-wcs-download-handler.php#L232C25-L232C59)

$existing_permissions = $wpdb->get_results( $wpdb->prepare( "SELECT order_id, download_id from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d", $product_id ) );

Product impact

Additional context

I made this fix. https://gist.github.com/lordspace/f3c56d64fd6e22f7e1126d9fd0472257