Cannot retrieve home url with custom queries

home_url() uses get_home_url() which in turn uses get_option( 'home' ) to retrieve the home url. The code in get_option() includes this:

// If home is not set use siteurl.
if ( 'home' == $option && '' == $value )
    return get_option( 'siteurl' );

I didn’t unwind all the previous code in the function, but it seems likely that under some circumstances get_option( 'siteurl' ) does not match home_url(). Apparently, you found one of those circumstances. Try using site_url() in this case.