WordPress.org

Ready to get started?Download WordPress

Forums

[closed] Admin-ajax.php being Called from Admin Pages Causing DB Connection Issues (39 posts)

  1. optimized-marketing
    Member
    Posted 9 months ago #

    Here is some back story, my webhosting shut all my sites down because admin-ajax.php was creating DB connections and sleep for long periods of time. The highest sleep time I saw was 45s. What I determined is that in one of the WP updates, from WP 3.5 to WP 3.7.1, admin-ajax.php started being called from multiple admin pages (/wp-admin/widgets.php, /wp-admin/index.php, /wp-admin/themes.php, and a few more) ever 2.5-3 min, even if nothing on the pages was touched. While this issue doesn't cause any problems if you only have one admin page open and close it after each use. But when you have multiple pages open and leave them open all day or over night WP will continue to make the POST to admin-ajax.php. After multiple hours (10+ when it is easiest to see) all of these continual POSTs will starts to negatively impact MYSQL. Database connection from admin-ajax.php will just sleep for multiple seconds even thought nothing is going on.

    For reference In WP 3.5 admin-ajax.php was only called automatically on the post.php page (I believe for autosave purposes). Also on 3.7.1 I switched to the default twenty thirteen theme and deactivated all the plugins and the auto POST things is still occurring.

    What is the point of admin-ajax.php being called on pages where no information was change and no ajax was initiated? Is there a way to turn this off? Is this a feature of WP and expected functionality?

    Thanks
    Greg

  2. Scriptrunner (Doug Sparling)
    Member
    Posted 9 months ago #

    It's most likely the WordPress Heartbeat API introduced in WP 3.6.

    Using the WordPress Heartbeat API

    The Heartbeat API: Changing the Pulse

    I've not done this myself, so I'm not sure if this is the complete solution, but you should be able to add this to functions.php to stop the API:

    remove_action( 'admin_init', 'wp_auth_check_load' );

  3. I've managed to bring down server servers by using the Heartbeat API on the frontend. You need to be very, very careful with it.

  4. optimized-marketing
    Member
    Posted 9 months ago #

    Thanks, Someone else mentioned the Heartbeat API as a possible culprit. I will remove the action and see if that prevents it from being called. Will let you know how it goes.

    Greg

  5. optimized-marketing
    Member
    Posted 9 months ago #

    I added remove_action( 'admin_init', 'wp_auth_check_load' ); to my functions.php file yet WP is still loads admin-ajax.php same as it did before. ever 2.5-3 min it does a POST

    It does seem to be related to heartbeat though. here is the postdata: POSTDATA=data%5Bwp-auth-check%5D=true&interval=15&_nonce=628a115b24&action=heartbeat&screen_id=theme-editor&has_focus=true

    Since that didnt work any other ideas?

  6. Scriptrunner (Doug Sparling)
    Member
    Posted 9 months ago #

    Yes, given the time interval and given the log snippet you posted, I'd guess with confidence that it's the heartbeat api. (that's the normal time interval, though I do think it slows down after a period of inactivity) It's not something I've actually tried to do, and I wasn't 100% sure if what I gave you would work.

    Is there a reason you need to have multiple admin pages open constantly? The obvious and easiest solution is not to do that.

    Maybe Pippin or someone else knows, but I'm not sure how to disable it (or if you really should), but I may look into it when I have more time over the weeekend...

  7. optimized-marketing
    Member
    Posted 9 months ago #

    That is just how I work on WP sites, I may also be working on multiple WP sites at one time,which are hosted on the same server. I like to have multiple tabs open for each area so I don't have to keep reloading pages or re-navigate to them for the needed information. Efficiency Reasons.

    You are right just have one or two open is the easiest and most obvious solution along with closing down the admin section whenever not in use. That is a Process/Systems Solution I am going to implement. I don't see that as a fix to the issue, because if someone accidentally leave some open over night or during the day, I don't want all my sites to go down again.

    My ultimate goal is to talk to a WP developers to see if this is a known or expected experience. If not maybe add something to stop the heartbeat after a certain sleep setting or combine all the heartbeats into POST or something.

    Thanks for the help, hopefully someone else has some other ideas.

  8. Scriptrunner (Doug Sparling)
    Member
    Posted 9 months ago #

    Good luck - sounds like an edge case, but a perfectly valid one. AFAIK, the heartbeat only stops when you close your browser, but does slow down after (I think) five minutes of inactivity. But I'm not aware of any built-in way to turn it off. Also, as I think Pippin was alluding to, plugin developers do have the ability to decrease the ping time through a hook, down to once every 10-15 seconds I believe.

  9. Yes, that's correct. The pulse rate can be changed from a plugin.

  10. optimized-marketing
    Member
    Posted 9 months ago #

    I talked to someone working on heartbeat performance. They told me they have a few plans for a future WP update. They did give me away to turn it off. They suggested keep it on for the post page and new post page, so autosave continues to work.

    Add the following to functions.php to turn off heartbeat:

    add_action( 'init', 'my_deregister_heartbeat', 1 );
    function my_deregister_heartbeat() {
    	global $pagenow;
    
    	if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
    		wp_deregister_script('heartbeat');
    }
  11. rickydwilson
    Member
    Posted 7 months ago #

    [Content removed by poster]

  12. Steve
    Member
    Posted 6 months ago #

    So did this code work for anyone?? I'm leery of adding this code unless can definitively say it works :-) Thanks

  13. eventosesportivosbrasil
    Member
    Posted 6 months ago #

    Yes, It works!

    I was experiencing the same problem... Too slow when browsing the site and even more slow when using the admin page.

    I do not have use for the auto save option, so disabling it is not a problem at all.

    I just used the above code in both my sites and the problem is resolved!

    http://www.copamundial2014brasil.com.br and http://www.precodehotel.com.br.

  14. Page404
    Member
    Posted 6 months ago #

    Is the code supposed to be added to the wordpress' core functions.php or the theme's functions.php. which is which??

  15. esmi
    Forum Moderator
    Posted 6 months ago #

    Your theme's functions.php file. You should never touch WordPress core files.

  16. Page404
    Member
    Posted 6 months ago #

    Thanks, esmi for the quick response

  17. scmg
    Member
    Posted 6 months ago #

    1) OK - I took the code mentioned above ---

    add_action( 'init', 'my_deregister_heartbeat', 1 );
    function my_deregister_heartbeat() {
    global $pagenow;

    if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
    wp_deregister_script('heartbeat');
    }

    2) pasted it into my functions file

    3) ran a speed test at pingdom http://tools.pingdom.com/fpt/#!/ejOa0r/http://www.halfpipelondon.com/product-category/bike/bicycles/womens-bicycles/

    4) still getting this ajax problem

    5) anyone got more ideas

    Ian

  18. MarkLL
    Member
    Posted 6 months ago #

    It's a pity that the default behavior for this functionality is not OFF.

    We have a dedicated server with > 200 members and one member left his browser sitting on the profile page for 3 days. The end result was after the weekly log rotation process Apache could not write to the new log files. Took me a couple of days to figure out what was going on.

    Only resolved the issue by restarting Apache.

    I've since implemented the following in the themes function.php (similar to above, but works)

    function my_deregister_heartbeat() {
    	global $pagenow;
    
    	if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
    		wp_deregister_script('heartbeat');
    		wp_register_script('heartbeat', false);
    	}
    }
    add_action( 'admin_enqueue_scripts', 'my_deregister_heartbeat' );

    Sorry guys, but in it's current form this feature is a serious brain-fart!

  19. robelia
    Member
    Posted 5 months ago #

    It works!! Great

  20. robelia
    Member
    Posted 5 months ago #

    One site down by the script. (Internal Server Error) Is not a great solution in the end.

  21. laurabfont
    Member
    Posted 5 months ago #

    the code did not work for me - are there any other fixes out there?

  22. laurabfont
    Member
    Posted 5 months ago #

    i disabled the new order email notification and this SIGNIFICANTLY improved the load time. The load time is still not ideal but this is the only thing that has seemed to work. Please note, I did not disable customer email notifications, just the email that gets sent to me when a user submits an order. Easy fix and very helpful.

    Please note I tried all of the following, but the only thing that helped was email deactivation.

    My server is digitalocean 2GB Ram 30GB SSD Disk.

    I have tried:

    1) deregistering the heartbeat api
    2) increasing the memory limit (96M) and php memory limit (64M)
    3) my WP URL and site URL match
    4) switched to default wp theme (no change)
    5) same issue on http and https
    6) logged in and non logged in - issue persists

    disabling the email seemed to be the thing that fixed this issue

  23. zumzam123
    Member
    Posted 5 months ago #

    I have the same problem!

    The code is not working for me. Deactivating the email function cannot be the solution, have not tried it yet but I want to keep this function alive.

    The load time of the admin-ajax.php takes somewhere between 1 and 1,6 seconds!

    Have also a VPS 2gb ram and wordpress gets 128mb of it.

    Would be nice to get help on this issue.

  24. poddys
    Member
    Posted 4 months ago #

    Where do you add this into the Theme's functions./php file, at the beginning, end or before the first function definition?

    Our backend is incredibly slow and this is the first real option I have seen that has the potential to make a big difference.

  25. robelia
    Member
    Posted 4 months ago #

    No good recommendation for this problem so far.

  26. chinaskl1
    Member
    Posted 4 months ago #

    Mi página ver películas en linea también tiene el mismo problema... active el código en function.php pero igualmente continua lento. Y ya me bloquearon de mi hosting

  27. Robbie
    Member
    Posted 4 months ago #

    I Just can NOT understand there is NO solution for this BIG problem.

    http://wordpress.org/support/topic/admin-ajaxphp-very-long-loading-time-woocommerce?replies=8"

    http://wordpress.org/support/topic/admin-ajaxphp-slow-server-response-time?replies=28"

    This solution in my theme functions also does not work:

    add_action( 'init', 'my_deregister_heartbeat', 1 );
    function my_deregister_heartbeat() {
    global $pagenow;

    if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
    wp_deregister_script('heartbeat');
    }

    PLEASE HELP !!

  28. robelia
    Member
    Posted 4 months ago #

  29. Skoad
    Member
    Posted 4 months ago #

    Also been having this issue. Have searched all over the place with the only answer being to disable the heartbeat. Ive done that and no change was made. Pingdom still showing adam-ajax taking up to 30 seconds at times (generally takes about 6-10seconds). It must be a plugin but ive tried disabling them and still having the issue. Going nuts.

  30. Skoad
    Member
    Posted 4 months ago #

    It wouldnt let me edit my post so having to post again...

    I seemed to have fixed the problem and narrowed it down to one of two plugins I had.

    So I updated to workpress 3.9 today and my site didnt like it. LayerSlider bugged out and would no longer show, and my tmeplate hasnt updated yet.

    I also noticed there was an update for Better Security, so I updated that. My site definitely didnt like that update as the whole site bugged out. Couldnt even reach my dashboard.

    So I went into my FTP and removed Better Security and LayerSlider since I couldnt use it at the moment.. then after that, admin-ajax doesnt even show up on pingdom anymore.

    Soooo.. I guess the problem is fixed?

Topic Closed

This topic has been closed to new replies.

About this Topic